How to make radio text struts create a vertical list of radio buttons

I use the struts radio tag, which is populated with a list of objects that have two fields:

class 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 is what I used on my page to display a list of radio buttons

 <s:radio key="selectedId" list="myList" listKey="id" listValue="name"/> 

However, this does give a horizontal list of switches. I tried adding css style to them:

 <style> .vertical input { display: block; } </style> 

But this leads to the fact that labels and radio buttons are displayed on separate lines, and not on the switch and label on the same line:

first object second object third object

I want:

first object second object third object
+6
source share
4 answers

its actually simple, I mean the theme of using simple :)

 <s:iterator value="myList"> <s:radio theme="simple" name="someNameToSubmit" list="#{id:name}"/><br> </s:iterator> 

This will make name as label and id as property to send

+12
source

after some searching around the world ... I found several solutions:

  • Change the theme and change struts FTL for radio buttons: Instructions are here . It seemed redundant to me - or at least I'm too lazy for that.

  • Use an iterator tag, iterate over each item in the list, and output one switch and line breaks for each item in the list. The answer came from here.

I chose option two (because I'm lazy in the first place), although option one would be done for a good exercise.

This is what my struts tag looks like:

 <s:iterator value="myList"> <s:radio key="selectedId" list="{myObject}" listKey="id" listValue="name"/><br/> </s:iterator> 

So, the iterator works in List, so you set the list attribute of the radio tag as a list containing only the current myObject. ListKey and listValue are myObject.id and myObject.name

+4
source

I have a simple solution. In the list, add <br> to each element, for example,

 first object <br> 

It works, although it looks like a hack.

+3
source

You can also just use the map in Java code. It will also help get rid of the MyObject class. Sort of:

 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 // ... } 
0
source

Source: https://habr.com/ru/post/886821/


All Articles