如何在动作类和jsp页面之间传递对象数据? [英] How to pass object data between action class and jsp page?

查看:174
本文介绍了如何在动作类和jsp页面之间传递对象数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个名为Code的Java类.它具有与代码相关的所有值,如codeIdcodeDescription等,以及其getter和setter方法.我正在一个动作类中成功检索Code的数据(我正在使用struts 2).

I am having a Java class named Code. It has all the values related to code like codeId, codeDescription etc with their getters and setters. I am retrieving the data of the Code in one action class successfully (I am using struts 2).

现在,我想在我的display.jsp页面中获取这些值.显然,我希望显示来自codeobj.codeIdcodeobj.codeDescription等对象的数据.我该怎么做?

Now I want to get these values in my display.jsp page. Clearly I want the data from the object like codeobj.codeId, codeobj.codeDescription etc to be displayed. How I can do this?

推荐答案

您所需要做的就是Action类中字段的getter和setter方法. Struts2会将对象放在ValueStack的顶部,借助OGNL,您可以从JSP访问属性.

All you need to have is getter and setter methods for the fields in your Action class. Struts2 will put that object on the top of ValueStack and With the help of OGNL you can access the properties from JSP.

这是代码段

public class Test Extends ActionSupport{
    public String execute() throws Exception{
     // Action Logic fetching/Init code object
     return SUCCESS;

}

private Code code=null;

public void setCode(Code code){
    this.code=code
}

public Code getCode(){
    return code;
}

}

现在Struts2框架会将code实例放在ValueStack的顶部,在该位置上,所有请求处理数据都由该框架放置,并由jsp/Actions使用OGNL(导航语言)进行引用来获取数据.

Now Struts2 framework will put the code instance on the top of ValueStack which is the place where all request processing data is being placed by the framework and being referred by the jsp/Actions using OGNL which is a navigation language to fetch the data.

在您的JSP中,您可以使用以下代码访问code实例

in your JSP you can access the code instance with the following code

<s:property value="%{code.codeId}"/>
or
<s:textfield name="abc" value="%{code.codeId}"/>

这里真正发生的是框架将code实例的填充值放在ValueStack上,借助OGNL,我们正在获取该值.

what exactly happening here is that framework has placed your code instance with the filled value on the ValueStack and with the help of OGNL we are fetching that value.

OGNL将检查在值堆栈顶部是否有命名为code的实例,该实例将由框架放置,在找到code实例后,它将检查其是否具有codeId属性.找到该属性后,OGNL将进行数据类型转换并在JSP中显示该值.

OGNL will check if there is an instance namd code on the top of value stack which will be placed by framework, after it found code instance it will check if it has codeId property. On finding the property, OGNL will do the data type conversion and will show the value in JSP.

希望这会对您有所帮助.

hope this will help you.

这篇关于如何在动作类和jsp页面之间传递对象数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆