我试图理解别人的Struts 2代码,我遇到了数据传递问题。
我知道在JSP页面上,如果使用<s:textfield name="something" ... />标记,struts2将尝试在动作类中自动调用setSomething(...) 。
我现在看到这种类型的代码:
<s:textfield name="item.name" ... />而且我想知道,这是怎么回事. (点)工作? 我在我的动作类中有一个名为setItem()的方法,并且在该方法中设置的对象具有setName()方法,但显然这不起作用。 点和名称之间的点是什么意思,我如何正确地使用它来实例化项目并设置它的名称?
PS:我的动作类中的setItem()中设置的item对象有一个空的args构造函数。
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.
最满意答案
在OGNL . 是点符号 。
item.name表示getItem().setName(); item.subitem.name表示getItem().getSubitem.setName();
一个问题可能是缺少空args构造函数, 如此处所述 ,但您说这不是您的情况; 然后我打赌“缺少Item吸气剂”。 如果不是,请发布更相关的代码。
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.
更多推荐
发布评论