在Spring MVC应用程序中, MultiActionController用于将相关动作分组为单个控制器,方法处理程序必须遵循以下签名:
public (ModelAndView | Map | String | void) actionName(
HttpServletRequest, HttpServletResponse [,HttpSession] [,CommandObject]);
1. MultiActionController
请参见MultiActionController示例。
package com.mkyong.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>
2.映射示例
现在,重新请求的URL将以以下模式映射到方法名称:
- 客户总监–> / customer / *
- / customer / 添加 .htm –> add()
- / customer / delete .htm –> delete()
- / customer / update .htm –> update()
- / customer / list .htm –> list()
3. InternalPathMethodNameResolver
InternalPathMethodNameResolver是默认的MultiActionController实现,用于将URL映射到方法名称。 但是,您仍然可以在方法名称中添加前缀或后缀:
<beans ...>
<bean
class="org.springframework.web.servlet.mvc.support.ControllerClassNameHandlerMapping" />
<bean class="com.mkyong.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将以以下模式映射到方法名称:
- 客户总监–> / customer / *
- / customer / 添加 .htm –>测试添加 Customer()
- / customer / delete .htm –>测试delete Customer()
- / customer / update .htm –>测试更新 Customer()
- / customer / list .htm –>测试listC ustomer ()
注意
使用注释,可以轻松配置MultiActionController ,请访问此MultiActionController注释示例以获取详细信息。
下载源代码
下载它– SpringMVC-MultiActionController-Example.zip (7KB)
参考文献
标签: 春天MVC
翻译自: https://mkyong.com/spring-mvc/spring-mvc-multiactioncontroller-example/