Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to pass an object from a JSP form to an Action class in Struts2 without defining all form fields as a class property?

To make the question clear I make an example:

  1. consider theSubscriber form defined bellow in first.jsp:

    <s:form id="theSubscriber" name="theSubscriber" method="post" action="next.action">
        <s:bean name="my.hibernate.actors.Subscriber" var="user">
            <s:hidden name="id" key="id" value="%{user.id}"  />
            <s:textfield id="subscriberForename" name="forename"
                        key="forename" value="%{user.forename}" label="Forename" />
            <s:textfield id="subscriberSurname" name="surname" 
                        key="surname" value="%{user.surname}" label="Surname" />
        </s:bean>
    </s:form>
    
  2. consider the following action class for the next.action

    public class NextAction extends ActionSupport {
    
        private Subscriber user = new Subscriber();
    
        private String forename;
    
        public String getForename() {
            return forename;
        }        
        public void setForename(String forename) {
            this.forename = forename;
        }
    
        public ManageSubscriber() {
            // TODO Auto-generated constructor stub
        }
    
        public ManageSubscriber(Subscriber user) {
            this.user = user;
        }
    
        public Subscriber getUser() {
            return user;
        }                
        public void setUser(Subscriber user) {
            this.user = user;
        }
    
        public String execute() {
            System.out.println(getUser());//This prints out null
            System.out.println(getForename());//This prints out the posted forename
            return SUCCESS;
        }
    }
    
  3. The question is: I know that defining all the form fields as action class properties let the class fill them correctly. But I want to make it fill the relevant fields in another class which contains all necessary properties. The user/subscriber class is like this:

    public class User {
        private long id;
        private String username;
        private String password;
        private UserLevel userLevel;
        private String forename;
        private String surname;
        private String email;
        private String phoneNumber;
        private String address;
        private Date birthday;
        private Date registrationDate;
    }
    

I have defined all accessor methods. In fact the problem is that it looks very annoying and redundant to define all these fields for the nextAction class and then evaluate the instance of user in that class.

How should I solve this problem?

like image 556
Johnny Avatar asked Dec 30 '12 01:12

Johnny


People also ask

How send data from action class to JSP in struts?

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.

How do you pass value from one action to another in Struts2?

This is called Action chaining in Struts 2. One action leads to another one and so on. Request > Action 1 > Action 2 > Response In Struts 2, this can be achieved by Chain Result. The Chain Result is a result type that invokes an Action with its own Interceptor Stack and Result.


1 Answers

It is easier than you think.

You are very close, and you've already done it for value attribute (used to pre-set the value for the item on the page): now just do it for name attribute (used to send the value to the Action), like this:

<s:hidden name="user.id" key="id" value="%{user.id}"  />
<s:textfield id="subscriberForename" name="user.forename"
    value="%{user.forename}" label="Forename" />
<s:textfield id="subscriberSurname" name="user.surname" 
    value="%{user.surname}" label="Surname" />

(Key attribute was not necessary here)

Note that for this to work, you will need a bean with a no-args constructor (as you have).

Avoid initializating it yourself too.

Change this:

private Subscriber user = new Subscriber();

to this

private Subscriber user;
like image 52
Andrea Ligios Avatar answered Oct 01 '22 15:10

Andrea Ligios