简体   繁体   English

如何让Struts2仅在POST上验证

[英]How to let struts2 only validate on POST

for example, I have an add method, which in "get" show a form 例如,我有一个add方法,该方法在“ get”中显示一个表单

and in "post" do the add logic, that is validate, call service method and etc. 并在“发布”中执行添加逻辑,即验证,调用服务方法等。

in the "get" phase, I don't want validate 在“获取”阶段,我不想验证

but the struts2 always validate in those two phase. 但是struts2始终在这两个阶段中进行验证。

How can I config struts2 to only validate on "post" ? 如何配置struts2仅在“ post”上验证?

Forget about the REST behavior call the same URL and do different things basing on the Http verbs you are used to: 忘了REST行为调用相同的URL,并根据您惯用的Http动词做不同的事情

@RequestMapping(value = "/foo", method = RequestMethod.GET)
public ModelAndView ReadFoobar(Locale locale, 
                               Model model, 
                               HttpServletRequest request){
    String foobar = getService().loadFoobar();
    ModelAndView mav = new ModelAndView("foobarPage")
    mav.addObject("foobar",foobar);
    return mav;
}

@RequestMapping(value = "/foo", method = RequestMethod.POST)
public String InsertStuff(@RequestParam("stuff") String stuff,
                          Locale loc, 
                          Model model,
                          HttpServletRequest req){
    validate();
    getService().insertStuff(stuff);
    return "stuffPage";
}

That is Spring MVC, this is Struts2. 那就是Spring MVC,这就是Struts2。 You need to reason in terms of Actions; 您需要根据“动作”进行推理; instead of working with Http verbs, use different Actions and define the validation for only one of them. 与其使用Http动词,不如使用不同的动作并仅对其中一个定义验证。 For example: 例如:

Action representing GET 代表GET的动作

public class ReadFoobar {

    @Getter private String foobar;

    public String execute(){
        foobar = getService().loadFoobar();
        return SUCCESS;
    }

}

Action representing POST 代表POST的动作

public class InsertStuff {

    @Setter private String stuff = "";

    public String execute(){
        getService().insertStuff(stuff);
        return SUCCESS;
    }

    public void validate(){
        if (stuff.length()<3){
            addFieldError("stuff","The mininum length is 3");
        }
    }
}

At this point, associate your readAction only to GET requests: 此时,将您的readAction仅与GET请求相关联:

<s:a action="ReadFoobar">check out this Foobar</s:a>

and insertAction only to POST requests: 而insertAction仅用于POST请求:

<s:form action="InsertStuff" theme="simple">
    <s:textfield name="stuff" />
    <s:fielderror fieldName="stuff" />

    <s:submit value="Insert the stuff" />
</s:form>

You can do the same with XML Validation by specifying a different XML file for each Action class; 您可以通过为每个Action类指定一个不同的XML文件来对XML验证执行相同的操作。 You can also have two methods in the same Action class, and specify different XML validation (or no validation) at method level, by renaming the XML with the method name after the action name. 您还可以在同一个Action类中有两个方法,并通过在操作名称后使用方法名称重命名XML,从而在方法级别指定不同的XML验证(或不进行验证)。

Finally, if you still want to be SURE that absolutely no requests, even when cratfed to bypass your UI, can reach eg. 最后,如果您仍然想确保绝对没有请求,即使在绕过UI时也无法做到。 ReadFoobar in GET mode, you can write a simple Interceptor (or two if you don't want to pass GET / POST as parameter) and apply it to the desired Action(s). 在GET模式下的ReadFoobar中,您可以编写一个简单的拦截器(如果不想通过GET / POST作为参数,则可以编写两个)并将其应用于所需的Action。

Eg. 例如。 write a BlockGetRequestsInterceptor that will block all the GET requests, and apply it to InsertStuff Action (either by struts.xml or with annotations, if using the Convention plugin, that I recommend): 编写一个BlockGetRequestsInterceptor,它将阻止所有GET请求,并将其应用于InsertStuff Action(我建议使用struts.xml或带有批注(如果使用Convention插件,则建议使用批注)):

public class InsertStuff {

    @Action(interceptorRefs={
        @InterceptorRef("BlockGetRequestsInterceptor"), 
        @InterceptorRef("defaultStack")})
    public String execute(){
        getService().insertStuff(stuff);
        return SUCCESS;
    }

    // ... 

I like Spring MVC too, but IF you want / need / are using Struts2, use it the right way, the way it is designed for, otherwise it could turn into a nightmare ;) 我也喜欢Spring MVC,但是如果您想要/需要/正在使用Struts2,请以正确的方式使用它,按照设计的方式使用它,否则可能会成为一场噩梦;)

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

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