0

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.

  • What is null, why is null? Did you check `createIfNull` option? The question should be updated to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. – Roman C Jul 05 '22 at 19:43
  • 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. – Shambhavi R Jul 06 '22 at 09:07
  • I don't know what you mean. It looks like you don't understand what I'm talking. I will give you a [link](https://stackoverflow.com/a/23829108/573032) to read. Don't forget to upvote it if it helped. – Roman C Jul 06 '22 at 10:01
  • Yeah the scenario is kind of similar in this link as well. But in my case, instead of having 2 actions mapped to one Action CLass, I have 2 different Action Classes. And the annotations solution does not work for me. So how to get the Array of Object values in the 2nd Action class from the JSP redirection of the 1st Action class? – Shambhavi R Jul 13 '22 at 07:00
  • If it doesn't work for you then your option to find a solution yourself. l can't help you in any way. – Roman C Jul 13 '22 at 10:13
  • Thank you for the help. I did find a solution. Using session to store objects works. I'm now able to get the required Array of Object values using session. – Shambhavi R Jul 13 '22 at 18:03
  • Using session to store objects is not good for the workflow design. Especially in struts2 where the model is used on the view layer. And these model objects are lived in the action scope. Using a session in this case is not appropriate. You also said that on the view you have used hidden properties, but in your JSP there's no such properties. You have not stored them anyware neither you submitted them to the action. So your question has no sense from the basic MVC interaction with the client. When you submit a list of properties basically a type conversion involved under the box and indexing. – Roman C Jul 14 '22 at 16:52
  • Oh okay yeah I missed adding the hidden properties part since it was not working. Okay I get what you are saying. Thanks. – Shambhavi R Jul 21 '22 at 05:00

0 Answers0