我们如何限制Struts2 Action仅适用于Post
方法?
答案 0 :(得分:10)
你为什么要这样做?
除此之外,你可以做到这一点......
//Following has not been tested
package com.quaternion.interceptor;
import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.AbstractInterceptor;
import javax.servlet.http.HttpServletRequest;
import org.apache.struts2.ServletActionContext;
public class PostOnlyInterceptor extends AbstractInterceptor {
@Override
public String intercept(ActionInvocation ai) throws Exception {
HttpServletRequest request = ServletActionContext.getRequest();
if (!request.getMethod().equals("POST")){
return Action.ERROR;
}
return ai.invoke();
}
}
然后为特定包构建一个拦截器堆栈,并将您的操作放在该包中,或者使用注释将您的操作与包关联使用ParentPackage注释。