How can I use List of object with Stripes “option” tag?
문제
I have a List of object, produced by JPA q.getResultList()
.
I would like to use it in a drop down, but Stripes "option" tag cant accept List, just Collection
, Enum
and
Map
.
Im new to Java, that why perhaps the List
can translated to each of them but I don't know how can I solve this issue.
(Stripes select,option-map,-enumeration, -collection can build up a drop down from previous mentioned input object structures )
해결책
The documentation of the options-collection tag says:
Writes a set of
<option value="foo">bar</option>
tags to the page based on the contents of aCollection
,Iterable
orArray
. Each element in the collection is represented by a single option tag on the page. Uses the label and value attributes on the tag to name the properties of the objects in theCollection
that should be used to generate the body of the HTML option tag and the value attribute of the HTML option tag respectively. If either (or both) of the label or value properties are omitted the item itself will be used for the label/value instead - this is done to support collections of simple types like Strings and Numbers.E.g. a tag declaration that looks like:
<stripes:options-collection collection="${cats}" value="catId" label="name"/>
would cause the container to look for a
Collection
called "cats" across the various JSP scopes and set it on the tag. The tag would then proceed to iterate through that collection callinggetCatId()
andgetName()
on each cat to produce HTML option tags.
A java.util.List
being a Collection
, just pass it to the collection
attribute of the mentioned tag.
다른 팁
그러나 놀이 때문에! Pojo Binding 와 같은 일을하는 것이 좋습니다.
public static void add(Bookmark newBookmark) {
// Validation etc ..
bookmark.save();
}
.
GSON으로 정말로 작업하기를 원한다면 이런 일이 일할 수 있습니다 ( 문서 ) :
public static void add(JsonObject body) {
Gson gson = new Gson();
String json = gson.toJson(body, Bookmark.class);
Bookmark bookmark = gson.fromJson(json, Bookmark.class);
bookmark.save();
}
.