javaparametersstruts2ognl

How to get URL parameter in prepare method of action class in Struts 2


I'm getting null as URL parameter name in prepare method of Preparable interface, while parameter is set in the URL.

URL I am trying to visit:

http://localhost:8080/basic-struts/registerInput.action?userid=1
public class Register extends ActionSupport implements Preparable {    

    private String userid;       

    public void prepare() throws Exception {
        // Call the service, load data, 
        // every time even if validation fails

        System.out.println("----------");
        System.out.println(userid); //<-------null
        System.out.println("----------");
    }

    public String getUserId() {
        return userid;
    }

    public void setUserId(String userid) {
        this.userid = userid;
    }

}

Note, I get some error in logs:

2021-07-01 09:28:53,929 ERROR [qtp1182469998-22] interceptor.ParametersInterceptor (ParametersInterceptor.java:238) - Developer Notification (set struts.devMode to false to disable this message): Unexpected Exception caught setting 'userid' on 'class org.apache.struts.register.action.Register: Error setting expression 'userid' with value ['1', ]


Solution

  • @Dave Newton's suggestion helped, on the link he shared I got the clue and got it working,

    I changed my struts.xml and added interceptor paramsPrepareParamsStack to my action as follows,

    <action name="registerInput" class="org.apache.struts.register.action.Register" method="input" >
        <result name="input">/register.jsp</result>
        <interceptor-ref name="paramsPrepareParamsStack"/>
    </action>