[ https://issues.apache.org/jira/browse/WW-5145?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=17617822#comment-17617822 ]
Lukasz Lenart commented on WW-5145: ----------------------------------- Checkbox tag was designed to work with {{Boolean}} so supporting {{List}}/{{Map}} can be a bit tricky. You can use {code:html} <s:checkbox name="options[0]" fieldValue="1"/> <s:checkbox name="options[1]" fieldValue="2"/> <s:checkbox name="options[2]" fieldValue="3"/> {code} which should do the trick. Also you have a full control over the layout by overriding theme and you can just override one tag if you want. > Checkbox with multiple values do not default correctly > ------------------------------------------------------ > > Key: WW-5145 > URL: https://issues.apache.org/jira/browse/WW-5145 > Project: Struts 2 > Issue Type: Bug > Components: Core Tags > Affects Versions: 2.5.25 > Reporter: nikos dimitrakas > Priority: Minor > Labels: checkbox > Fix For: 6.1.0 > > > Using multiple s:checkbox (with the same name) together with a List (of > Integer or String, etc) does not preselect the right checkboxes. This > behaviour is inconsistent with all the other form components that get the > default values based on their name. I have constructed a simple example for > illustrating and testing. My actual case is more complex with dynamic data. > Action class: > {code:java} > public class TestAction extends ActionSupport { > private List<Integer> number; > > public List<Integer> getNumber() { > return number; > } > > public void setNumber(List<Integer> number) { > this.number = number; > } > public String execute() { > //Try with some default, empty list, null > if (number == null) number = Collections.singletonList(2); > return SUCCESS; > } > } > {code} > JSP: > {code:html} > <%@ taglib prefix="s" uri="/struts-tags_rt" %> > <html> > <body> > <s:form action="test"> > 1 <s:checkbox name="number" fieldValue="1"/> > 2 <s:checkbox name="number" fieldValue="2"/> > 3 <s:checkbox name="number" fieldValue="3"/> > 4 <s:checkbox name="number" fieldValue="4"/> > <button type="submit">Push</button> > </s:form> > </body> > </html> > {code} > Action configuration: > {code:xml} > <action name="test" class="TestAction" method="execute"> > <result name="success">test.jsp</result> > </action> > {code} > One would expect that the checkboxes should be checked if the corresponding > value is in the list. But instead all checkboxes get checked if the list is > not null (independent of its content). And all checkboxes are unchecked if > the list is null. Submitting the form after selecting some checkboxes does > send the values correctly to the action, but once again when rendering the > checkboxes, they all get checked if the list is not null. > I can simply fix this by adding the value attribute with an expression to > each checkbox, but it feels like this should be the default behaviour: > {code} > 1 <s:checkbox name="number" fieldValue="1" value="number.contains(1)"/> > 2 <s:checkbox name="number" fieldValue="2" value="number.contains(2)"/> > 3 <s:checkbox name="number" fieldValue="3" value="number.contains(3)"/> > 4 <s:checkbox name="number" fieldValue="4" value="number.contains(4)"/> > {code} > Another workaround is to use a Map<Integer, Boolean> and in that case all > checkboxes are checked and unchecked correctly, but it feels unnecessary > since every entry will always have value true and the only interesting part > of the map will be the keySet (which is essentially the list). But it is > interesting that the checkboxes behave as expected when using a Map, but not > when using a List. > I am not really sure if this relates to WW-4865 -- This message was sent by Atlassian Jira (v8.20.10#820010)