本文介绍了将 Struts2 操作限制为仅发布方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

How can we restrict a Struts2 Action to work only for Post method?

推荐答案

为什么要这样做?

除此之外,您还可以这样做...

That aside here is how you could do it...

//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 注释将您的操作与包相关联.

Then build an interceptor stack for a particular package and put your actions in that package or with annotations associate your action with the package using the ParentPackage annotation.

这篇关于将 Struts2 操作限制为仅发布方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-18 03:06
查看更多