You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@struts.apache.org by "Lukasz Lenart (Jira)" <ji...@apache.org> on 2021/11/12 14:18:00 UTC

[jira] [Updated] (WW-5145) Checkbox with multiple values do not default correctly

     [ https://issues.apache.org/jira/browse/WW-5145?page=com.atlassian.jira.plugin.system.issuetabpanels:all-tabpanel ]

Lukasz Lenart updated WW-5145:
------------------------------
    Fix Version/s:     (was: 2.5.28)

> 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: 2.6
>
>
> 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:
> {{public class TestAction extends ActionSupport {}}
> {{ private List<Integer> number;}}
> {{ public List<Integer> getNumber() { }}
> {{return number;}}
> {{}}}{{public void setNumber(List<Integer> number) {}}
> {{ this.number = number;}}
> {{ }}}{{@Override}}
> {{ public String execute() {}}
> {{//Try with some default, empty list, null}}
> {{ if (number == null) number = Collections.singletonList(2);}}
> {{ return SUCCESS;}}
> {{ }}}
> {{}}}
> {{JSP:}}
> <%@ 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>
> Action configuration:
> {{ <action name="test" class="TestAction" method="execute">}}
> {{ <result name="success">test.jsp</result>}}
> {{ </action>}}
> 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:
> {{ 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)"/>}}
> {{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.1#820001)