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

To make the question clear, I will give an example:

  • consider the form of theSubscriber defined below 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> 
  • consider the next class of actions for 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; } } 
  • Question: I know that defining all form fields as properties of an action class allows the class to fill them correctly. But I want him to fill in the appropriate fields in another class that contains all the necessary properties. The user / subscriber class is as follows:

     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 access methods. Actually, the problem is that it is very difficult and redundant to define all these fields for the nextAction class, and then evaluate the user instance in this class.

How do I solve this problem?

+4
source share
2 answers

It is easier than you think.

You are very close, and you already did this for the value attribute (used to pre-set the value for the element on the page): now just do this for the name attribute (used to send the value to the action), for example:

 <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 is not needed here)

Please note that for this you will need a bean with the no-args constructor (like yours).

Avoid initializing it yourself.

Change this:

 private Subscriber user = new Subscriber(); 

to that

 private Subscriber user; 
+8
source

I think you are asking how you can store a User object in memory that will be used for several actions.

You have several options if I understand your question correctly.

One option that is not very similar to Struts is to add a User object to the session variable. You can access the session variable as described here .

Another option that I would recommend if you plan to maintain an object in your session for a specific set of actions (or steps, for example, part of the "wizard" of your application) is ScopeInterceptor. You can read here here and here .

Hope this helps.

+1
source

All Articles