Struts 2将数据从JSP传递给动作类(Struts 2 passing data from JSP to action class)

编程入门 行业动态 更新时间:2024-10-24 09:29:45
Struts 2将数据从JSP传递给动作类(Struts 2 passing data from JSP to action class)

我试图理解别人的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.

更多推荐

本文发布于:2023-07-18 04:41:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1154916.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:动作类   数据   JSP   Struts   class

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!