Yichz
Yichz

Reputation: 9681

How Struts 2 handle request parameters

I remember I've read this from somewhere but still unsure.

consider scenario below:

<action name="doSomething" class="com.domain.MyAction" method="myMethod">              
    </action> 

and

 public class MyAction extends ActionSupport{

      public String myMethod(){
       private String param;

       }
       //getter
       //setter
    }

then via web page I do POST o GET: domain/doSomething?param=hello

I can recover "param" value using any method below, are they the same?

this.getParam();
(String)request.getParameter("param");
(String)request.getSession.getAttribute("param");

if so, then struts will always put request parameters into http_session?

Upvotes: 1

Views: 3981

Answers (1)

coding_idiot
coding_idiot

Reputation: 13734

Struts uses getters and setters of request parameters to pass them in the action.

 public class MyAction extends ActionSupport{

   private String param;
   public void setParam(String p){ param=p;  }
   public String getParam() { return param; }

  public String myMethod(){
      System.out.println("Got the request parameter automatically just by having a getter and setter for that parameter " + param);
   }
}

Upvotes: 2

Related Questions