[ 
https://issues.apache.org/struts/browse/WW-1780?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel#action_40367
 ] 

Kris Coolsaet commented on WW-1780:
-----------------------------------

There is a fairly simple workaround for the 'checkboxes' use case of the first 
post. (But there are other use cases for which this is not a valid solution.)

Declare delete as a map property:

    private Map<Integer,Boolean> delete = new HashMap<Integer,Boolean>();

    public Map<Integer, Boolean> getDelete() {
        return delete;
    }

A checkbox with name delete[133] will then be registered as key '133' and value 
'true' or 'false' depending on whether the box was checked or not. Use an 
execute method like the following:

    public String execute() {
        for (int key : delete.keySet())
            if (delete.get(key))
                // remove record with the given key
        return SUCCESS;
    }

(This is Java 5.0 with autoboxing and for each loop.)

We are really misusing a map as a set here. For those of you who are not happy 
with this, note that you do not have to use a HashMap for the property delete - 
any map will do. You could write your own Map implementation which does not 
store the Boolean values and only stores those keys that correspond to 
checkboxes that are actually checked.

This would also get rid of the if in execute, You only need this if when using 
the struts checkbox-tag in the first place - not with hardcoded HTML-input 
checkboxes. (On the other hand, leaving it in does little harm, and allows your 
action to be reused in other contexts.)


> Indexed (bean) properties do not work when property is not a real array
> -----------------------------------------------------------------------
>
>                 Key: WW-1780
>                 URL: https://issues.apache.org/struts/browse/WW-1780
>             Project: Struts 2
>          Issue Type: Bug
>          Components: Actions, Value Stack
>    Affects Versions: 2.0.6
>            Reporter: Kris Coolsaet
>            Priority: Minor
>
> OGNL documentation states that for an indexed property (like test[133] ) an 
> indexed getter and setter of the form
>    public String getTest (int index)
>    public void setTest (int index, String value)
> should be sufficient. In other words: there need not be a property (test) 
> which corresponds to a real array of strings.
> This does not seem to work in Struts 2.
> Sample test case:
> Create an action class Test.action containing the following code
>     private String command = "no command";
>     
>     public String getCommand () {
>         return command;
>     }
>     public void setTest (int index, String value) {
>         command = "test["+index+"]="+value;
>     }
> and a JSP-page test.jsp which contains something like
>         <h1>
>             <s:property value="command"/>
>         </h1>
> Link them to an action Test in the struts.xml file
>         <action name="Test" class="Test">
>             <result>/test.jsp</result>
>         </action>
> and type in the following url: Test.action?test[133]=xxx
> The page displays 'no command' instead of 'test[133]=xxx' as I would expect.
> (A variant of this, using strings as indices instead of integers, does also 
> not work.)
> There are many use cases for this functionality. For example, consider a long 
> table with many columns, in which every row
> displays some element of a large database table (only a small selection of 
> the entire table is displayed). The leftmost column contains checkboxes. A 
> single 'delete'-button will be used to remove all checked items from the 
> database.
> In this case, it would be nice for the individual checkboxes to carry names 
> like 'delete[123]', 'delete[255]', ..., where the index is the unique 
> database key of the record. The 'setDelete(int index)' method of the action 
> could then simply add the sequence number to a set, while the execute method 
> could simply iterate over the set. There would then be no need for the action 
> to create a big array of booleans, one for each record in the database.

-- 
This message is automatically generated by JIRA.
-
You can reply to this email to add a comment to the issue online.

Reply via email to