I'm using a struts radio tag that is being populated with a list of objects that have two fields:
clas开发者_如何学Cs MyAction {
List<MyObject> myList;
String selectedId
public String execute() {
...
myList = new ArrayList<MyObject>();
myList.add(new MyObject("1","first object");
myList.add(new MyObject("2","second object");
myList.add(new MyObject("3","second object");
...
}
// Getters and Setters for myList & selectedId
...
}
class MyObject {
String id;
String name;
MyObject(String id, String name) {
this.id = id;
this.name = name;
}
// Getters and Setters for id & name
...
}
Here's what I was using on my page to display the list of radio buttons
<s:radio key="selectedId" list="myList" listKey="id" listValue="name"/>
However, this yields a horizontal list of radio buttons. I tried adding a css style to them:
<style>
.vertical input { display: block; }
</style>
But this causes the labels and the radio buttons to show up on separate lines as well, instead of the radio button and label on the same line:
first object second object third object
what I want is:
first object second object third object
its actually simple, i mean use theme simple
:)
<s:iterator value="myList">
<s:radio theme="simple" name="someNameToSubmit" list="#{id:name}"/><br>
</s:iterator>
This will make name
as a label and id
as the property to submit
after some googling around a bit... I found a few solutions:
Modify extend the theme and modify the struts FTL for radio buttons: Instructions here. This seemed overkill for me - or at least I'm too lazy for that :)
Use an iterator tag, iterate over each list item, and output one radio button and line break for each list element. Answer came from here
I chose option two (because I'm lazy primarily), although option one would make for a good exercise.
Here's what my struts tag looks like now:
<s:iterator value="myList">
<s:radio key="selectedId" list="{myObject}" listKey="id" listValue="name"/><br/>
</s:iterator>
So the iterator works on a List, so you set the list attribute of the radio tag to be a list of containing only the current myObject. The listKey and listValue are then myObject.id and myObject.name
I have a simple solution. In the list, add <br>
to each item such as,
first object <br>
It works though looks like a hack.
You could also just use a map in the Java code. That will also have the benefit of getting rid of the MyObject
class. Something like:
class MyAction {
Map<String, String> myMap;
String selectedId;
public String execute() {
// ...
myMap = new HashMap<String, String>();
myMap.put("1","first object");
myMap.put("2","second object");
myMap.put("3","second object");
// ...
}
// Getters and Setters for myMap & selectedId
// ...
}
精彩评论