将Struts2操作限制为仅发布方法

JR *_*lia 3 java struts2

我们如何限制Struts2 Action仅适用于Post方法?

Qua*_*ion 11

你为什么要这样做?

除此之外你是如何做到的......

//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();
    }  
}
Run Code Online (Sandbox Code Playgroud)

然后为特定包构建一个拦截器堆栈,并将您的操作放在该包中,或者使用注释将您的操作与包关联起来使用ParentPackage注释.