Spring MVC MultiActionController注释示例

在本教程中,我们向您展示如何使用@RequestMapping开发基于Spring MVC注释的MultiActionController

在基于XML的MultiActionController中,您必须配置方法名称解析程序( InternalPathMethodNameResolverPropertiesMethodNameResolverParameterMethodNameResolver )以将URL映射到特定的方法名称。 但是,有了注释支持,生活变得更加轻松,现在您可以将@RequestMapping注释用作方法名称解析器,该解析器用于将URL映射到特定方法。

注意
这个基于注释的示例是从最后一个基于 Spring MVC MultiActionController XML的示例转换而来的。 因此,请比较并找出不同之处。

要对其进行配置,请在方法名称上方使用映射URL定义@RequestMapping

package com.mkyong.common.controller;
 
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

@Controller
public class CustomerController{
 
	@RequestMapping("/customer/add.htm")
	public ModelAndView add(HttpServletRequest request,
		HttpServletResponse response) throws Exception {
 
		return new ModelAndView("CustomerAddView");
 
	}
 
	@RequestMapping("/customer/delete.htm")
	public ModelAndView delete(HttpServletRequest request,
		HttpServletResponse response) throws Exception {
 
		return new ModelAndView("CustomerDeleteView");
 
	}
 
	@RequestMapping("/customer/update.htm")
	public ModelAndView update(HttpServletRequest request,
		HttpServletResponse response) throws Exception {
 
		return new ModelAndView("CustomerUpdateView");
 
	}
 
	@RequestMapping("/customer/list.htm")
	public ModelAndView list(HttpServletRequest request,
		HttpServletResponse response) throws Exception {
 
		return new ModelAndView("CustomerListView");
 
	}
}

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

  1. /customer/add.htm –> add()方法
  2. /customer/delete.htm –> delete()方法
  3. /customer/update.htm –> update()方法
  4. /customer/list.htm –> list()方法

注意
在Spring MVC中,此@RequestMapping始终是最灵活和易于使用的映射机制。

下载源代码

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值