Reputation: 3736
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, thrn Struts 2 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 means between item
and name
, and how do I use it correctly to instantiate the item and set its name?
PS: The item object that is being set in setItem()
in my action class has an empty args
constructor.
Upvotes: 2
Views: 8363
Reputation: 3736
The problem is solved. The getItem() actually contained the following code:
public Item getItem()
{
System.out.println("Trying to get item: " + item.toString());
return item;
}
And this gave a nullpointer exception because item was null. Only, this nullpointerexception was NOT THROWN by the struts framework! The code just continued (and failed of course...). When I removed the sysout statement, the code worked.
Upvotes: 1
Reputation: 1
Struts2 treated value in the name
attribute "item.name"
like OGNL expression. But it doesn't substitute the result of evaluation to the name
attribute but to the value
attribute if it didn't set. Then if you submit the form the parameter from the name
attribute is created and sent via HTTP request. Struts2 used params
interceptor to parse parameter names. It treated such names like OGNL expression and call appropriate methods get
and set
while accessing objects and setting values. Dot in OGNL expression stands for property resolver. In your case you should have getItem()
method to set the value and this item should return not null
value. setItem()
is not used there. May be you ask why it's not used, but it would probably another question.
Upvotes: 1
Reputation: 45583
Make sure there is public method for getItem().setName()
Maybe the methods are not available or not public or a typo, for example getitem() instead of getItem().
Your action should have setItem(Item item)
Upvotes: 1
Reputation: 50281
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.
Upvotes: 3