Struts 2 passing data from JSP to action class

user1884155 picture user1884155 · Nov 22, 2013 · Viewed 7.6k times · Source

I'm trying to understand somebody's else Struts 2 code and I'm stuck with a data passing problem.

I know that on a JSP page, if you use a <s:textfield name="something" ... /> tag, than struts2 will try to call setSomething(...) automatically in the action class.

I'm now seeing this type of code:

<s:textfield name="item.name" ... />

and I'm wondering, how does this . (dot) work? I have a method called setItem() in my action class, and the object that is being set in that method has a setName() method, but apparently this doesn't work. What does the dot mean between item and name and how do I use it correctly to instantiate the item and set it's name?

PS: The item object that is being set in setItem() in my action class has an empty args constructor.

Answer

Andrea Ligios picture Andrea Ligios · Nov 22, 2013

In OGNL . is the dot notation.

item.name means getItem().setName();
item.subitem.name means getItem().getSubitem.setName();

One problem could be the missing empty args constructor, as described here, but you are saying it's not your case; then I bet on the "missing getter for Item". If it's not, please post more relevant code.