1

I was explaining to a colleague the way of getting list data from a JSP page to back to the action class by using indices as explained here and here. He didn't quite understand and fumbled a bit on his own until he suddenly he made it work by not using indices at all!

In his JSP page he had:

<input type="checkbox" name="contactNameList" value="someValue1">
<input type="checkbox" name="contactNameList" value="someValue2">
<input type="checkbox" name="contactNameList" value="someValue3">
<input type="checkbox" name="contactNameList" value="someValue4">

In his action class he had the 'appropiate' setters:

public List<String> getContactNameList()

public void setContactNameList(List<String> list)

I'm baffled as to why this work. I think this works because he is sending non-bean data (in this case strings) and there is an intelligence build into Struts2/OGNL to append values to lists rather than overwrite them.

Can anybody explain with great detail what is going behind the hood in this "no indices" case? How is the list of strings instantiated and populated with the snippets above?

Community
  • 1
  • 1
user1884155
  • 3,616
  • 4
  • 55
  • 108
  • 1
    Well why shouldn't it work? If you have a list you can always call `add` method to add one more element to the list, but what if you need to change/update specific element in that list (at some index)? How will you reference that element? – Aleksandr M Sep 05 '14 at 18:04
  • In the action class, the result is a list of strings. We don't need to reference any string specifically, just loop over entire collection. Anyway, I'm curious as to what happens in the java code. When and how is the add method called? How does struts2 know it should NOT overwrite the list every time with a new fresh empty list? when using indices this is clear, but without I don't see it. – user1884155 Sep 05 '14 at 18:21
  • What do you mean? It **IS** re-created every time you call an action. With indexes or without it doesn't matter. – Aleksandr M Sep 05 '14 at 18:24
  • Get away from your coworker. He's h4x00r and will bring pain and curses to your workgroup. :P – Andrea Ligios Sep 05 '14 at 22:12

2 Answers2

1

You should understand that bean data and not bean data are passed as parameters to the action. The parameters has a structure that you can find if you implement ParameterAware.

Note that all parameter values for a given name will be returned, so the type of the objects in the map is java.lang.String[].

Then XWork Type Conversion make its best to convert this map to beans properties. See Built in Type Conversion Support.

Routine type conversion in the framework is transparent. Generally, all you need to do is ensure that HTML inputs have names that can be used in OGNL expressions. (HTML inputs are form elements and other GET/POST parameters.)

In the no indexes case parameters are mapped under the one key, rather than indexed names are used under their own names.

Roman C
  • 49,761
  • 33
  • 66
  • 176
-1

You have to set index value in above code

like

<input type="checkbox" name="contactNameList[0]" value="someValue1">
xrcwrn
  • 5,339
  • 17
  • 68
  • 129
  • No, that's the whole point of this question, you DONT have to set an index for it to work :), and I think its weird. – user1884155 Sep 06 '14 at 08:50
  • No, you don't; when you don't, S2 notices there's more than one value, iterates, and adds to the collection. – Dave Newton Sep 06 '14 at 17:55