【问题标题】:Restrict Struts2 Action to Post Method Only将 Struts2 操作限制为仅发布方法
【发布时间】:2012-11-12 01:40:24
【问题描述】:

我们如何限制 Struts2 Action 只对 Post 方法起作用?

【问题讨论】:

    标签: java struts2


    【解决方案1】:

    为什么要这样做?

    除了这里是你如何做到这一点......

    //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 注释的包关联。

    【讨论】:

      猜你喜欢
      • 2021-02-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-03-16
      • 2011-05-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多