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.
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 asuserName
anduserAge
.
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
.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.