4

我今天遇到MultiActionController了,我不确定我是否理解它的用途......在 Spring文档上,它说

允许同一类处理多种请求类型的控制器实现。这个类的子类可以处理几种不同类型的请求,方法如下:

在另一个网站上,他们说

它将多个请求处理方法聚合到单个控制器中,因此可以轻松地将相关功能保持在一起。

我仍然不明白如何扩展MultiActionController比以下更好或不同?或者MultiActionController更多的是旧版本 Spring 的遗产?我觉得我有一些基本的误解......

@Controller
public class MyController {

    @RequestMapping("/SomePage.html")
    public ModelAndView epdReview(
        @RequestParam(value="sessionID", required=true) String sessionId,
        MyFormData form,
        HttpSession session,
            HttpServletRequest request){
        //....
    }

    @RequestMapping(value = "/newData.json", method = RequestMethod.POST)
    public @ResponseBody ValidationResponse process(Model model,
        @Valid SomeData formData, BindingResult result,
        HttpSession session) {
        //....
    }
}
4

5 回答 5

3

Yes, that is from the Spring 2.0 MVC API, as is anything that implements the old Controller Interface. I can't find anything around the web from a Spring Developer regarding why this one was not deprecated when all of its cousins were.

I could offer 100% pure speculation that it could be because people could have totally valid custom-implementations of the MethodNameResolver that are impossible to replace with the behaviour of DefaultAnnotationMethodHandlerAdapter. Therefore annotation based config does not provide a 100% alternative to every legitimate use of this class.

于 2013-03-15T18:36:01.260 回答
1

Spring 提供了一个多动作控制器,通过它可以将多个动作聚合到一个控制器中,将功能分组在一起。多动作控制器能够将请求映射到方法名称,然后调用正确的方法名称。当您在一个控制器中有很多常用功能,但又希望控制器有多个入口点时,使用多动作控制器特别方便

于 2015-11-26T09:09:58.080 回答
0

使用带注释的控制器的漏洞——就像你后面的例子一样——是支持请求处理而不依赖于继承或框架特定的类(比如第一个例子中的四个)。在Spring 文档中,您可以找到以下内容:

Spring 2.5 为 MVC 控制器引入了基于注解的编程模型,使用 @RequestMapping、@RequestParam、@ModelAttribute 等注解。这种注解支持可用于 Servlet MVC 和 Portlet MVC。以这种风格实现的控制器不必扩展特定的基类或实现特定的接口。

因此,如果您已经在使用带注释的控制器,那么扩展 Spring 内置控制器(如MultiActionController.

于 2013-03-15T18:45:53.527 回答
0

Spring MultiActionController 用于将类似的操作映射到单个控制器。所以如果你使用 MultiActionController 类,你不需要为每个动作创建新的控制器类。

例如:当我们登录网上银行时,有一个添加和删除收款人的选项。因此我们可以使用单个 PayeeController 来执行类似的操作,例如添加和删除。

于 2015-12-16T07:22:00.630 回答
-1

引自mkyong.com

在 Spring MVC 应用程序中,MultiActionController 用于将相关操作分组到单个控制器中,方法处理程序必须遵循以下签名:

public (ModelAndView | Map | String | void) actionName(
    HttpServletRequest, HttpServletResponse [,HttpSession] [,CommandObject]);

例子

package com.vitthal.common.controller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.multiaction.MultiActionController;

public class CustomerController extends MultiActionController{

    public ModelAndView add(HttpServletRequest request,
        HttpServletResponse response) throws Exception {

        return new ModelAndView("CustomerPage", "msg","add() method");

    }

    public ModelAndView delete(HttpServletRequest request,
        HttpServletResponse response) throws Exception {

        return new ModelAndView("CustomerPage", "msg","delete() method");

    }

    public ModelAndView update(HttpServletRequest request,
        HttpServletResponse response) throws Exception {

        return new ModelAndView("CustomerPage", "msg","update() method");

    }

    public ModelAndView list(HttpServletRequest request,
        HttpServletResponse response) throws Exception {

        return new ModelAndView("CustomerPage", "msg","list() method");

    }

}

配置了 ControllerClassNameHandlerMapping。

<beans ...>

 <bean 
  class="org.springframework.web.servlet.mvc.support.ControllerClassNameHandlerMapping" />

  <bean class="com.mkyong.common.controller.CustomerController" />

</beans>

映射示例 现在,请求的 URL 将按照以下模式映射到方法名称:

CustomerController –&gt; /customer/*
/customer/add.htm –&gt; add()
/customer/delete.htm –&gt; delete()
/customer/update.htm –&gt; update()
/customer/list.htm –&gt; list()

InternalPathMethodNameResolver InternalPathMethodNameResolver 是将 URL 映射到方法名称的默认 MultiActionController 实现。但是,您仍然可以在方法名称中添加前缀或后缀:

<beans ...>
 <bean 
  class="org.springframework.web.servlet.mvc.support.ControllerClassNameHandlerMapping" />

  <bean class="com.vitthal.common.controller.CustomerController">
     <property name="methodNameResolver">
    <bean class="org.springframework.web.servlet.mvc.multiaction.InternalPathMethodNameResolver">
       <property name="prefix" value="test" />
       <property name="suffix" value="Customer" />
    </bean>
     </property>
   </bean>
</beans>

现在,URL 将按照以下模式映射到方法名称:

CustomerController –&gt; /customer/*
/customer/add.htm –&gt; testaddCustomer()
/customer/delete.htm –&gt; testdeleteCustomer()
/customer/update.htm –&gt; testupdateCustomer()
/customer/list.htm –&gt; testlistCustomer()

注意 有了注解,MultiActionController 更容易配置

于 2013-03-15T18:35:05.847 回答