3

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

然而,Spring MVC 现在使用这些简洁的注解@RequestMapping来处理请求。 @RequestMapping对于某人是否使用 GET 或 POST 有一个明显的过滤器,但我看不到对 SimpleFormController 提供的所有有用逻辑的任何内在支持。我仍然可以使用注释吗?

4

4 回答 4

1

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

第一种方法是继续在类级别使用SimpleFormControllerwith with注释。@RequestMapping@RequestMapping 的一个鲜为人知但非常酷的特性是它非常清楚如何处理实现 SpringController接口的类。这里唯一的缺点是我仍在使用旧的 MVC 接口和类,它们将在 Spring 3.0 中被弃用。

上面的kgiannakakis指出了第二种方式。只需为可以调用提交的每种方式创建一个@RequestMapping方法,并让它们都调用一个提交方法,无论是构造函数链接样式还是使用一些私有方法。简单易懂。谢谢,吉安纳卡基斯!

于 2009-05-18T08:46:27.640 回答
0

从这里复制:

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 选项来定义所需的功能。Annotations Controller 没有实现任何可使用的接口。

于 2009-05-13T07:43:29.750 回答
0

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

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

只有在 POST 中有一个名为“formAction”且值为“APPROVE”的字段时,才会调用此方法。

于 2009-08-17T15:17:33.573 回答
0

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

但是,如果您想在使用 @InitBinder 注释的方法上实现相同的功能,您可以简单地执行以下操作:

@InitBinder
public void initBinder(HttpServletRequest request) {
    if ("POST".equals(request.getMethod()){
        //Do something
    }
}
于 2009-09-15T00:00:04.077 回答