Spring MVC MultiActionController示例

在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将以以下模式映射到方法名称:

  1. 客户总监–> / customer / *
  2. / customer / 添加 .htm –> add()
  3. / customer / delete .htm –> delete()
  4. / customer / update .htm –> update()
  5. / 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将以以下模式映射到方法名称:

  1. 客户总监–> / customer / *
  2. / customer / 添加 .htm –>测试添加 Customer()
  3. / customer / delete .htm –>测试delete Customer()
  4. / customer / update .htm –>测试更新 Customer()
  5. / customer / list .htm –>测试listC ustomer ()

注意
使用注释,可以轻松配置MultiActionController ,请访问此MultiActionController注释示例以获取详细信息。

下载源代码

下载它– SpringMVC-MultiActionController-Example.zip (7KB)

参考文献

  1. MultiActionController Javadoc
  2. InternalPathMethodNameResolver Javadoc

翻译自: https://mkyong.com/spring-mvc/spring-mvc-multiactioncontroller-example/

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值