Shameer
Shameer

Reputation: 233

How to map request parameter to a bean with different name in struts2

I know Struts 2 make automatic mapping from request parameter to action class fields using param interceptor. But what if I want to map a parameter to an action field with different name. Suppose I have

<input type="text" name="username">

if I want to map this to the below field

private String realName;
public String getRealName() {
return realName;
}

public void setRealName(String realName) {
      this.realNaame = realName;
}

How can I do this mapping. Can I use

realName = request.getParameter("username");

If so how can I get the request object in the action class? Also will it work in the JSP page with the OGNL expression username or realName ? Is there any other way in struts2 configuration to do this kind of mapping?

Upvotes: 3

Views: 1705

Answers (1)

Dave Newton
Dave Newton

Reputation: 160191

Use the alias interceptor, nutshell:

<action name="someAction" class="com.examples.SomeAction">
    <!-- The value for the foo parameter will be applied as if it were named bar -->
    <param name="aliases">#{ 'foo' : 'bar' }</param>

    <interceptor-ref name="alias"/>
    <interceptor-ref name="defaultStack"/>
    <result>result.jsp</result>
</action>

Upvotes: 4

Related Questions