1

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)?

3
  • Use links for remove actions. Aug 5, 2014 at 7:27
  • Whether right or wrong, there's a design philosophy here that links provide navigation without performing action on data and that buttons are used to perform actions on data. I agree that using links would be much easier to implement but I also see the wisdom in the design philosophy.
    – tzimnoch
    Aug 5, 2014 at 17:24
  • You can easily style a link to look like a button. You are over complicating simple things. Aug 5, 2014 at 20:08

2 Answers 2

1

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.

5
  • I tried this syntax but it is not working. My rendered HTML is: <input type="submit" value="Remove" name="removeButtons['GN 00501.013']" id="removeButtons['GN 00501.013']"/>. The java code is the same as above except that I have to initialize as new HashMap<String,String>(). When I click the button, removeButtons.size() equals 0. Is there perhaps a Struts 2 setting necessary to process this parameter? I removed my interceptor definitions and went with the default stack and it still doesn't work. This is struts 2.3.16.1
    – tzimnoch
    Aug 5, 2014 at 17:30
  • @tzimnoch Before saying it doesn't work did you try the above solution?
    – Roman C
    Aug 5, 2014 at 17:52
  • I did. When it didn't work I looked at the HTML. I got <input type="submit" id="<actionName_###>" value="Remove"/>. Struts2 does not render an input tag with a name without providing a name attribute. Since I only want to submit the value for the button when that button is pressed I removed the hidden field and put that code directly in the submit tag. That still didn't work. I tried each in turn: removeButtons[string], removeButtons['string'], removeButtons(string), and removeButtons('string'). What is the HTML for the input tag you get that works with Map<String, String>?
    – tzimnoch
    Aug 5, 2014 at 18:11
  • No, you didn't. No such html in my code. You don't follow me, I just give up.
    – Roman C
    Aug 5, 2014 at 18:23
  • I did indeed try this and have found the eventual issue.
    – tzimnoch
    Aug 5, 2014 at 18:34
0

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. i.e. '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.

1
  • 1
    No, dots aren't allowed as well as spaces. But you can use acceptParamNames parameter to params interceptor to overcome the hardcoded pattern like in this answer. In all cases it's your responsibility to configure this interceptor, default configuration is for common usecases and not particularly yours.
    – Roman C
    Aug 5, 2014 at 19:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.