Take the 2-minute tour ×

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

share|improve this question

3 Answers 3

up vote 6 down vote accepted

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"
<s:textfield id="subscriberSurname" name="user.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;
share|improve this answer
Thanks. Così funziona bene! :D You pointed the problem very accurately! –  Johnny Dec 30 '12 at 21:06
Ottimo, you're welcome :) –  Andrea Ligios Dec 30 '12 at 21:46

I think you are asking how you can persist in memory a User object that will be used across multiple actions.

You have a couple of options if I understand your question correctly.

One option, which is not very Struts like, would be to add the User object to a session variable. You can access the session variable as described here.

Another option, which I would recommend if you plan on maintaining the object in your session for a specific set of actions (or steps, such as a 'wizard' portion of your app) is the ScopeInterceptor. You can read about that here and here.

Hope that helps.

share|improve this answer
Thank you! Your post was generaly useful for me and I learnt some interesting things but I didn't mean such this thing. I'm looking to know if struts has an automated way for initializing a java object defined as an attribute in an action class! If you have a look at my example it will become more obvious. I want to avoid redefining all user class attributes in the action class. I expect struts to do it for me! –  Johnny Dec 30 '12 at 2:31

The action class is placed to the root of the valueStack, so any attributes of the action could be accessed directly by the interceptors or referenced them as properties.

This is used in the scenario where you has migrated old gangster forms from Struts 1 into Struts 2 and not using Struts 1 plug-in. And such a form remains a concept of sharing between actions. If you are don't accept the idea of placing all the actions in the one action class this could be useful.

I know that currently any newbie starting to learn Struts looking for the newer version. But Struts2 is not a Struts, Struts2 initially named WebWork and concurrently won the name of Struts. About successive frameworks Struts 1 is I know but others are future will show us. Struts2 is not a Struts 2 and not backward compatible to Struts or Struts 1 but has successive migration strategy. That's what my answer about.

share|improve this answer
hmmm. In fact I have never used struts1.x. I work with struts since less than 10 days. Honestly I didn't get what you meant. –  Johnny Dec 30 '12 at 21:05
but you have mentioned it in the question and in the tag. –  Roman C Dec 30 '12 at 21:28
I have mentioned nothing in the question referring to any special version of struts. And in the tags I have tagged it generally to struts. I don't know if the issue is substantially different in these two versions of struts and generally it doesn't seem to me. In fact the description of struts tag in stackoverflow is "Apache Struts is an open source web application framework for the Java platform." and my question is related either to struts2 or any other previous or successive version because a good software is backward compatible and saying struts I never meant strutsX(X is a number). –  Johnny Dec 30 '12 at 21:41
Anyway I thank you a lot for trying to help me for solving the question! –  Johnny Dec 30 '12 at 21:42
Thanks for your comment on the difference between struts2 and struts. I have defined User because it is a part of my model(Referring to MVC concepts). I need to persist it. It will be used for some other actions and if I hadn't defined it as a separate class, it would have become extremely dirty and redundant to define the persistence xmls.(Even I wonder if it were possible) –  Johnny Dec 31 '12 at 11:59

Your Answer


