I'm working an a project where I need the Array of Objects values from One Action Class to another.
The flow goes something like this: Try1.java --> first.jsp --> Try2.java
I'm not able to get the Array of Objects values in Try2.java. It's always null.
User.java
package com.sham;
public class User {
private int id;
private String name;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
Try1.java
package com.sham;
import java.util.ArrayList;
import com.opensymphony.xwork2.ActionSupport;
public class Try1 extends ActionSupport{
ArrayList<User> list=new ArrayList<User>();
public ArrayList<User> getList() {
return list;
}
public void setList(ArrayList<User> list) {
this.list = list;
}
public String execute(){
User user1=new User();
user1.setId(123);
user1.setName("John");
list.add(user1);
User user2=new User();
user2.setId(345);
user2.setName("Jen");
list.add(user2);
System.out.println("List " + list);
return "success";
}
}
struts.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<package name="default" extends="struts-default">
<action name="click" class="com.sham.Try1" >
<result name="success">first.jsp</result>
</action>
<action name="next" class="com.sham.Try2" >
<result name="success">second.jsp</result>
</action>
</package>
</struts>
first.jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<s:form action="next">
Hello
<s:submit value="submit"/>
</s:form>
</body>
</html>
Try2.java
package com.sham;
import java.util.ArrayList;
import com.opensymphony.xwork2.ActionSupport;
public class Try2 extends ActionSupport {
ArrayList<User> list;
public ArrayList<User> getList() {
return list;
}
public void setList(ArrayList<User> list) {
this.list = list;
}
public String execute(){
System.out.println("List from Action class 2 " + getList());
return "success";
}
}
On clicking the submit button in first.jsp, the control goes to the Try2.java action class.
In Try2.java, I get null in getList().
I mean to say that, generally having getters and setters would work to get the values from one Action Class to another using hidden properties. But, in case of Array of Objects, it does not work the same, and the values are null.
In this case I want to get the Array of Object values for user 1 and user 2 in Try2.java.
I want to know how to get the same Array of Objects in the Try2.java from Try1.java action class.