简体   繁体   中英

Struts 2: How to handle indexed-property buttons on a form

I need to implement a table where each row has editable information that the user expects to submit as a whole yet buttons on each row to delete that row immediately. Because I care about the form as a whole and forms cannot be nested, the action receiving the button press probably has to handle it as an indexed property so that's the way I built it.

What I am currently doing is indexing via a Long.

My jsp renders the following html:

<input type="submit" value="Remove" name="removeButtons[## numbers go here ##]" />

While in my action class I am exposing and using the property like so:

private Map<Long, String> removeButtons = new HashMap<Long, String>();
public Map<Long, String> getRemoveButtons() {
    return removeButtons;
}

// Later when the action is called
for(Long button : removeButtons.keySet()) {
    // this only ever returns nothing or the one button that was pressed
}

This works perfectly fine for numerical indices.

I now need to do this again for a different table, but the rows are indexed with a String. Converting Long to String, putting or not putting quotes inside the square brackets, changing the square brackets to parentheses... Nothing seems to work.

I already know how to make it work with numbered indices and I'm capable of implementing it by adding a numerical index value to each row, however, I'd like to know how to make this work with a String key for the Map.

Alternately, is there a better way to achieve what I'm trying to do (arbitrarily many buttons on a single form)?

This getter you need if you want to use quotes in indexes. It allows to use String keys to the Map and you should use quotes in indexes to let OGNL to use string values as keys and evaluate corresponding getter.

private Map<String, String> removeButtons = new HashMap<>();
public Map<String, String> getRemoveButtons() {
    return removeButtons;
}

For example

<s:hidden name="removeButtons['0']" />
                               ^ ------ //the string key
<s:submit value="Remove" />

or use the string variable, or property of the action

<s:set var="idx" value="'0'"/>
                         ^ ------ //the string key
<s:hidden name="removeButtons[%{#idx}]" />                               
<s:submit value="Remove" />

EDIT:

Who knows that you will use non-standard keys for the parameters that doesn't pass the default accepted pattern. Your parameters names like "removeButtons['GN 00501.013']" .

Use the pattern "\\\\w+((\\\\['\\\\w+((\\\\s\\\\w+)|(\\\\.\\\\w+))*'\\\\]))*" to acceptParamNames parameter of params interceptor to overcome the hardcoded pattern like was done in this answer.

The syntax Roman provides is correct, but there is a caveat, perhaps a bug.

In further debugging, I changed getRemoveButtons to

public Map<String, String> getRemoveButtons() {
    log.debug("Call to 'RemoveButtons'");
    return removeButtons;
}

I got the appropriate log message when using Long, but when I converted to Map, no syntax I've tried would get this function called until I discovered that white space in the string breaks the functionality. ie 'GN 00501.013' would not work, but 'GN00501.013' does. I'll enquire on the Struts2 mailing lists if this is by design or some other issue unrelated to Struts that cannot be overcome.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM