java 如何在动作类和jsp页面之间传递对象数据?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5374768/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 10:49:38  来源:igfitidea点击:

How to pass object data between action class and jsp page?

javajspstruts2

提问by adn295

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

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

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

现在我想在我的display.jsp页面中获取这些值。显然,我想从喜欢的对象中的数据codeobj.codeIdcodeobj.codeDescription来显示等。我怎么能做到这一点?

回答by Umesh Awasthi

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

您需要的只是Action类中字段的 getter 和 setter 方法。Struts2 将把那个对象放在最上面ValueStackOGNL你可以在 JSP的帮助下访问这些属性。

Here is the code snippet

这是代码片段

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;
}

}

Now Struts2 framework will put the codeinstance on the top of ValueStackwhich 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.

现在Struts2框架会把code实例放在上面ValueStack,所有的请求处理数据都被框架放置并被jsp/Actions使用OGNL引用,OGNL是一种获取数据的导航语言。

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

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

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

what exactly happening here is that framework has placed your codeinstance with the filled value on the ValueStackand with the help of OGNL we are fetching that value.

这里究竟发生了什么是框架已经将您的code实例与填充的值放在一起,ValueStack并且在 OGNL 的帮助下,我们正在获取该值。

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

OGNL 将检查code值堆栈顶部是否有一个实例 namd将被框架放置,在找到code实例后,它将检查它是否具有 codeId 属性。找到属性后,OGNL将进行数据类型转换并在 JSP 中显示值。

hope this will help you.

希望这会帮助你。

回答by xrcwrn

Try this

试试这个

<s:iterate value="codeobj" var="obj"

<s:property value="codeId"/>
<s:property value="codeDescription"/>

</s:iterate>

回答by Maged

To Pass data from an action to the jsp: Consider that you want to pass the message from the action to the jsp. First, declare the message as a String variable in the action class. In the Execute method, add the value of the message as showing in the example below:

将数据从动作传递到 jsp: 考虑您要将消息从动作传递到 jsp。首先,在操作类中将消息声明为字符串变量。在 Execute 方法中,添加消息的值,如下例所示:

public class TestAction {

String message;
public String execute () {


    System.out.println("Execute method called ");
    message = "SUCCESS Message Ya Maged";


    return "success";
}


public String getMessage() {
    return message;
}
public void setMessage(String message) {
    this.message = message;
}

}


in the JSP file: use the and the value parameter to get the value of that String message variable that you declared in the action class.

在 JSP 文件中:使用 和 value 参数获取您在操作类中声明的 String 消息变量的值。

<%@ taglib prefix="s" uri="/struts-tags"%>
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Insert title here</title>
</head>
<body>

<h1><s:property value="message"/></h1>

</body>
</html>