简体   繁体   English

如何在Struts2中强制方法仅接受POST参数?

[英]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. 我有以下方法,我需要强制该方法仅接受POST参数。 This method receives id of the selected user to retrieve its object. 此方法接收所选用户的ID以检索其对象。 I need to force this method to just accept posted ids not those sent by GET. 我需要强制此方法仅接受发布的ID,而不是GET发送的ID。

   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: 您还可以创建一个拦截器,以仅使用post方法检查所有您希望只使用的请求,请参见以下示例:

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

See the following link Restrict Struts2 action to post method only 请参阅以下链接,将Struts2操作限制为仅发布方法

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";
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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