2

I have an Action class named MyFirstClass in which I have a String variable and a User variable as follows:

public class MyFirstClass extends ActionSupport implements ModelDriven<User>,Preparable {

    User user;
    
    private String nickName;

    public void prepare(){
       user = new User();       
    }

    public User getModel(){
       return user;
    }
    .........................
    ........................    
}

The User class has String variables as userName and userAge.

As my Action class has implemented ModelDriven interface, the variables of the User class are supposed to get/set on the request.

I have a JSP file also which is designed as follows:

<s:form action="index">
<s:actionerror/>
<s:textfield name="myname" label="UserName:">
</s:textfield>
<s:textfield name="myage" label="UserAge:">
</s:textfield>
<s:submit key="submit" name="submit"/>
</s:form>

and struts.xml is designed as:

<package name="default" namespace="/" extends="struts-default">
        <action name="index" class="com.actionClasses.MyFirstClass">
            <param name="aliases">#{'myname':'nickName','myname':user.userName,'myage':user.age}</param>
            <interceptor-ref name="alias"/> 
            <interceptor-ref name="basicStack"/>        
            <result name="success">/success.jsp</result>
            <result name="input">/user.jsp</result>                
        </action>
</package>

My problem is:

Since my JSP page text fields' name do not match with the property names in the User class. I am not able to set the request parameters, like this, to the corresponding properties in the action with the help of alias interceptor.

Roman C
  • 49,761
  • 33
  • 66
  • 176
SagarS
  • 21
  • 1
  • 6

1 Answers1

1

Change the configuration for aliases to match property names, also use defaultStack you need it because it contains modelDriven interceptor.

You have stated

User class has String variables as userName and userAge.

If the form is like

user.jsp:

<s:form action="index">
<s:actionerror/>
<s:textfield name="name" label="UserName:">
</s:textfield>
<s:textfield name="age" label="UserAge:">
</s:textfield>
<s:submit key="submit" name="submit"/>
</s:form>

and result is

success.jsp:

<s:actionerror/>
<s:label name="userName" label="UserName:"/><br/>
<s:label name="userAge" label="UserAge:"/><br/>

the configuration should be:

<package name="default" namespace="/" extends="struts-default">
    <action name="index" class="com.actionClasses.MyFirstClass">
        <param name="aliases">#{'name':'userName','age':'userAge'}</param>
        <interceptor-ref name="defaultStack"/>        
        <interceptor-ref name="alias"/> 
        <result name="success">/success.jsp</result>
        <result name="input">/user.jsp</result>                
    </action>
</package>

The alias interceptor is at the end because it should be after modelDriven.

Roman C
  • 49,761
  • 33
  • 66
  • 176
  • userName and userAge are not the properties of my action class,they belong to the **User** class. Thats why,above solution is not working.Any other way.....?? – SagarS Nov 30 '13 at 17:21
  • I think this line,**#{'name':'userName','age':'userAge'}**, states that if the user submits a page,containing values for 'name' and 'age',then these values should hit the setters of 'userName' and 'userAge' of the landing action class(MyFirstClass) itself and not in any bean class. In my scenario,**MyFirstClass** is an action class and **User** is a bean class inside this action.....This soln. is still not working. – SagarS Nov 30 '13 at 20:56
  • 1
    Actually hit the `valueStack` and if your action is `ModelDriven` the model is affected. – Roman C Nov 30 '13 at 21:29