简体   繁体   中英

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

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).

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?

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.

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 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.

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}"/>

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 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.

Try this

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

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

</s:iterate>

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:

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.

<%@ 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>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM