简体   繁体   中英

How to force a method to only accept POST parameters in Struts2?

I have the following method, which I need to force to accept only POST parameters. This method receives id of the selected user to retrieve its object. I need to force this method to just accept posted ids not those sent by GET.

   public class Users{
     private long uid;

     public String show() {
            UsersModel usrModel = new UsersModel();
            return usrModel.retrieveUser(uid); //uid paramets will be sent by client to 
                                              //retrieve object of selected user
     }
     ....
   }

You can also create an interceptor to check for all request that you want to be only using post methods, see the following example:

HttpServletRequest request = ServletActionContext.getRequest();
...
request.getMethod().equals("POST") // check using this condition.

See the following link Restrict Struts2 action to post method only

try this:

String method = ServletActionContext.getRequest().getMethod();
if (method.equals("POST") {
    // do something
} else {}

try this,

 HttpServletRequest request=(HttpServletRequest) ActionContext.getContext().get(ServletActionContext.HTTP_REQUEST);
 if(request.getMethod().equals("POST"))
    {
             //your code
              return "accept";
    }else{
             return "not_accept";
    }

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM