繁体   English   中英

Spring MVC isFormSubmission()是否等效于注释?

[英]Spring MVC isFormSubmission() equivalent for annotations?

使用Spring MVC,很容易表达一个概念,例如“如果用户使用POST或包含'isSubmit'参数,则用户正在提交表单。” 您只需扩展SimpleFormController并覆盖isFormSubmission方法。

但是,Spring MVC现在使用这些简洁的注释(例如@RequestMapping来处理请求。 @RequestMapping有一个明显的过滤器,用于确定有人使用GET还是POST,但是我看不到对所提供的所有有用逻辑SimpleFormController的内在支持。 注释仍对我可用吗?

因此,经过一些调查,实际上有几种方法可以处理这种情况。

第一种方法是继续在类级别使用带有@RequestMapping批注的SimpleFormController @RequestMapping一个鲜为人知但很酷的属性是它非常清楚如何处理实现Spring的Controller接口的类。 唯一的缺点是我仍在使用旧的MVC接口和类,而在Spring 3.0中将不推荐使用它们。

上面的kiannakakis指出了第二种方法。 只需为构造函数调用样式或使用某些私有方法的每种可以调用提交的方式创建一个@RequestMapping方法,并使它们全部都调用一个提交方法即可。 简单易懂。 谢谢,kiannakakis!

这是使用路径映射的一个示例:

    @RequestMapping(params = "formAction=APPROVE", method = RequestMethod.POST)
    public String myMethod ()....

然后仅对POST调用此方法,在POST中存在一个名为“ formAction”且值为“ APPROVE”的字段。

列出的其他答案对于使用@RequestMapping注释的方法很好用。

但是,如果您想在使用@InitBinder注释的方法上实现相同的目的,则只需执行以下操作:

@InitBinder
public void initBinder(HttpServletRequest request) {
    if ("POST".equals(request.getMethod()){
        //Do something
    }
}

这里复制:

Path mappings can be narrowed through parameter conditions: a sequence of 
"myParam=myValue" style expressions, with a request only mapped if each such 
parameter is found to have the given value. "myParam" style expressions are 
also supported, with such parameters having to be present in the request
(allowed to have any value). Finally, "!myParam" style expressions indicate 
that the specified parameter is not supposed to be present in the request.

您只能使用RequestMapping选项定义所需的功能。 注释控制器未实现任何可使用的接口。

暂无
暂无

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

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