首页 Java java教程 SpringMVC学习系列(3) 之 URL请求到Action的映射规则

SpringMVC学习系列(3) 之 URL请求到Action的映射规则

Mar 03, 2017 am 10:49 AM
springmvc

在系列(2)中我们展示了一个简单的get请求,并返回了一个简单的helloworld页面。本篇我们来学习如何来配置一个action的url映射规则。

在系列(2)中我们在HelloWorldController上配置了一个@RequestMapping(value = "/helloworld")这表示对该controller的所有action请求必须是以"/helloworld”开始。

1.URL路径映射

1.1.对一个action配置多个URL映射:

我们把上一篇中的HelloWorldController的index() action方法的@RequestMapping更改为@RequestMapping(value={"/index", "/hello"}, method = {RequestMethod.GET}),这表示对该action配置了/index和/hello两个映射。运行测试,如下:

1

可以看到/helloworld/hello请求也成功匹配。

1.2.URL请求参数映射:

这在查询的时候经常用到,比如我们根据id或编号来获取某一条记录。

在HelloWorldController添加一个getDetail的action,代码如下:

@RequestMapping(value="/detail/{id}", method = {RequestMethod.GET})public ModelAndView getDetail(@PathVariable(value="id") Integer id){
    
    ModelAndView modelAndView = new ModelAndView();  
    modelAndView.addObject("id", id);  
    modelAndView.setViewName("detail");  
    return modelAndView;
}
登录后复制


其中value="/detail/{id}",中的{id}为占位符表示可以映射请求为/detail/xxxx 的URL如:/detail/123等。

方法的参数@PathVariable(value="id") Integer id 用于将URL中占位符所对应变量映射到参数id上,@PathVariable(value="id") 中value的值要和占位符/{id}大括号中的值一致。

在views中添加detail.jsp视图,用于将获取到的id值展示出来。视图内容如下:

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%><!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><html><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8"><title>Insert title here</title></head><body>
    ${id}</body></html>
登录后复制


运行测试,请求URL地址 http://localhost:8080/SpringMVCLesson/helloworld/detail/123 ,结果如下:

2

可以看到已经正确的显示了我们请求的id。

1.3.URL通配符映射:

我们还可以通过通配符对URL映射进行配置,通配符有“?”和“*”两个字符。其中“?”表示1个字符,“*”表示匹配多个字符,“**”表示匹配0个或多个路径。

例如:

“/helloworld/index?”可以匹配“/helloworld/indexA”、“/helloworld/indexB”,但不能匹配“/helloworld/index”也不能匹配“/helloworld/indexAA”;

“/helloworld/index*”可以匹配“/helloworld/index”、“/helloworld/indexA”、“/helloworld/indexAA”但不能匹配“/helloworld/index/A”;

“/helloworld/index/*”可以匹配“/helloworld/index/”、“/helloworld/index/A”、“/helloworld/index/AA”、“/helloworld/index/AB”但不能匹配 “/helloworld/index”、“/helloworld/index/A/B”;

“/helloworld/index/**”可以匹配“/helloworld/index/”下的多有子路径,比如:“/helloworld/index/A/B/C/D”;

如果现在有“/helloworld/index”和“/helloworld/*”,如果请求地址为“/helloworld/index”那么将如何匹配?Spring MVC会按照最长匹配优先原则(即和映射配置中哪个匹配的最多)来匹配,所以会匹配“/helloworld/index”,下面来做测试:

在HelloWorldController添加一个urlTest的action,内容如下:

@RequestMapping(value="/*", method = {RequestMethod.GET})public ModelAndView urlTest(){
    
    ModelAndView modelAndView = new ModelAndView();   
    modelAndView.setViewName("urltest");  
    return modelAndView;
}
登录后复制


在views文件夹中新加一个视图urltest.jsp,为了和index.jsp做区别urltest.jsp的内容如下:

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%><!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" 
    "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>Insert title here</title>
    </head>
    <body>
    urlTest!</body></html>
登录后复制


请求http://localhost:8080/SpringMVCLesson/helloworld/index查看结果:

3

可以看出映射的是index对应的action。

请求http://localhost:8080/SpringMVCLesson/helloworld/AAA查看结果:

4

可以看出映射的是urlTest对应的action。

1.4.URL正则表达式映射:

Spring MVC还支持正则表达式方式的映射配置,我们通过一个测试来展示:

在HelloWorldController添加一个regUrlTest的action,内容如下:

@RequestMapping(value="/reg/{name:\\w+}-{age:\\d+}", 
method = {RequestMethod.GET})public ModelAndView regUrlTest(@PathVariable(value="name") String name, 
@PathVariable(value="age") Integer age){
    
    ModelAndView modelAndView = new ModelAndView();   
    modelAndView.addObject("name", name); 
    modelAndView.addObject("age", age); 
    modelAndView.setViewName("regurltest");  
    return modelAndView;
}
登录后复制


在views文件夹中新加一个视图regurltest.jsp内容如下:

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%><!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" 
    "http://www.w3.org/TR/html4/loose.dtd"><html>
    <head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>Insert title here</title></head><body>
    ${name}-${age}</body></html>
登录后复制


请求http://www.php.cn/:8080/SpringMVCLesson/helloworld/reg/Hanmeimei-18查看结果:

5

请求http://www.php.cn/:8080/SpringMVCLesson/helloworld/reg/Hanmeimei-Lilei查看结果(会发现找不到对应的action返回404):

6

2.限制action所接受的请求方式(get或post):

之前我们在HelloWorldController的index() action方法上配置的为@RequestMapping(value="/*", method = {RequestMethod.GET})我们把它改为@RequestMapping(value="/*", method = {RequestMethod.POST})再次请求http://www.php.cn/:8080/SpringMVCLesson/helloworld/index试一下:

7

这里可以看到结果映射到了urlTest这个action,这是因为我们在urlTest上配置的为@RequestMapping(value="/*", method = {RequestMethod.GET}),当index这个action映射不在符合时便映射到了urlTest。

我们也可以这样配置@RequestMapping(value="/*", method = {RequestMethod.GET, RequestMethod.POST})表示该action可以接受get或post请求,不过更简单的是不对method做配置则默认支持所有请求方式。

3.限制action所接受请求的参数:

我们可以为某个action指定映射的请求中必须包含某参数,或必须不包含某参数,或者某参数必须等于某个值,或者某参数必须不等于某个值这些限制。

3.1.指定映射请求必须包含某参数:

在HelloWorldController添加一个paramsTest的action,内容如下:

@RequestMapping(value="/paramstest", params="example", 
method = {RequestMethod.GET})public ModelAndView paramsTest(){
    
    ModelAndView modelAndView = new ModelAndView();   
    modelAndView.setViewName("paramstest");  
    return modelAndView;
}
登录后复制


在views文件夹中新加一个视图paramstest.jsp内容如下:

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%><!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" 
    "http://www.w3.org/TR/html4/loose.dtd"><html><head><meta http-equiv="Content-Type" content="text/html; 
    charset=UTF-8"><title>Insert title here</title></head><body>
    paramstest!</body></html>
登录后复制


请求http://www.php.cn/:8080/SpringMVCLesson/helloworld/paramstest查看结果:

8

这里可以看到没有找到paramsTest这个action结果还是映射到了urlTest这个action。

请求http://www.php.cn/:8080/SpringMVCLesson/helloworld/paramstest?example查看结果:

9

这次可以看到请求映射到了paramsTest这个action。

3.2.指定映射请求必须包含某参数:

把刚才添加的paramsTest的@RequestMapping(value="/paramstest", params="example", method = {RequestMethod.GET}) 改为@RequestMapping(value="/paramstest", params="!example", method = {RequestMethod.GET})

重新请求http://www.php.cn/:8080/SpringMVCLesson/helloworld/paramstest?example查看结果:

10

可以看到又没有找到paramsTest这个action而映射到了urlTest这个action。

3.3.指定映射请求中或者某参数必须等于某个值:

把刚才添加的paramsTest的@RequestMapping(value="/paramstest", params="example", method = {RequestMethod.GET}) 改为@RequestMapping(value="/paramstest", params="example=AAA", method = {RequestMethod.GET})

请求http://www.php.cn/:8080/SpringMVCLesson/helloworld/paramstest?example=BBB查看结果:

11

可以看到没有找到paramsTest这个action而映射到了urlTest这个action。

请求http://localhost:8080/SpringMVCLesson/helloworld/paramstest?example=BBB查看结果:

12

这次可以看到请求映射到了paramsTest这个action。

3.4.指定映射请求中或者某参数必须等于某个值:

把刚才添加的paramsTest的@RequestMapping(value="/paramstest", params="example", method = {RequestMethod.GET}) 改为@RequestMapping(value="/paramstest", params="example!=AAA", method = {RequestMethod.GET})

请求http://localhost:8080/SpringMVCLesson/helloworld/paramstest?example=AAA查看结果:

13

可以看到请求映射到了paramsTest这个action。

请求http://localhost:8080/SpringMVCLesson/helloworld/paramstest?example=AAA查看结果:

14

可以看到没有找到paramsTest这个action而映射到了urlTest这个action。

注:当我们为params指定多个参数时如:params={"example1", "example2"},表示的是and关系,即两个参数限制必须同时满足。

 

4.限制action所接受请求头参数:

同限制action所接受的请求参数一样,我们也可以为某个action指定映射的请求头中必须包含某参数,或必须不包含某参数,或者某参数必须等于某个值,或者某参数必须不等于某个值这些限制。

4.1.指定映射请求头必须包含某参数:

@RequestMapping(value="/headerTest", headers = "example")。与限制请求参数是一样的,可以参考上面的例子进行测试。

4.2.指定映射请求头必须包含某参数:

@RequestMapping(value="/headerTest", headers = "!example")。与限制请求参数是一样的,可以参考上面的例子进行测试。

4.3.指定映射请求头中或者某参数必须等于某个值:

@RequestMapping(value="/headerTest", headers = "Accept=text/html")。与限制请求参数是一样的,可以参考上面的例子进行测试。

4.4.指定映射请求头中或者某参数必须等于某个值:

@RequestMapping(value="/headerTest", headers = "Accept!=text/html")。与限制请求参数是一样的,可以参考上面的例子进行测试。

注:当我们为headers指定多个参数时如:headers={"example1", "example2"},表示的是and关系,即两个参数限制必须同时满足。

 

 以上就是SpringMVC学习系列(3) 之 URL请求到Action的映射规则的内容,更多相关内容请关注PHP中文网(www.php.cn)!


本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn

热AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover

AI Clothes Remover

用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool

Undress AI Tool

免费脱衣服图片

Clothoff.io

Clothoff.io

AI脱衣机

AI Hentai Generator

AI Hentai Generator

免费生成ai无尽的。

热门文章

R.E.P.O.能量晶体解释及其做什么(黄色晶体)
3 周前 By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.最佳图形设置
3 周前 By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.如果您听不到任何人,如何修复音频
3 周前 By 尊渡假赌尊渡假赌尊渡假赌
WWE 2K25:如何解锁Myrise中的所有内容
1 个月前 By 尊渡假赌尊渡假赌尊渡假赌

热工具

记事本++7.3.1

记事本++7.3.1

好用且免费的代码编辑器

SublimeText3汉化版

SublimeText3汉化版

中文版,非常好用

禅工作室 13.0.1

禅工作室 13.0.1

功能强大的PHP集成开发环境

Dreamweaver CS6

Dreamweaver CS6

视觉化网页开发工具

SublimeText3 Mac版

SublimeText3 Mac版

神级代码编辑软件(SublimeText3)

SpringBoot与SpringMVC的比较及差别分析 SpringBoot与SpringMVC的比较及差别分析 Dec 29, 2023 am 11:02 AM

SpringBoot和SpringMVC都是Java开发中常用的框架,但它们之间有一些明显的差异。本文将探究这两个框架的特点和用途,并对它们的差异进行比较。首先,我们来了解一下SpringBoot。SpringBoot是由Pivotal团队开发的,它旨在简化基于Spring框架的应用程序的创建和部署。它提供了一种快速、轻量级的方式来构建独立的、可执行

比较SpringBoot与SpringMVC的差异是什么? 比较SpringBoot与SpringMVC的差异是什么? Dec 29, 2023 am 10:46 AM

SpringBoot与SpringMVC的不同之处在哪里?SpringBoot和SpringMVC是两个非常流行的Java开发框架,用于构建Web应用程序。尽管它们经常分别被使用,但它们之间的不同之处也是很明显的。首先,SpringBoot可以被看作是一个Spring框架的扩展或者增强版。它旨在简化Spring应用程序的初始化和配置过程,以帮助开发人

SpringBoot与SpringMVC的区别是什么? SpringBoot与SpringMVC的区别是什么? Dec 29, 2023 pm 05:19 PM

SpringBoot和SpringMVC是Java开发中常用的两个框架,它们都是由Spring框架所提供的,但在功能和使用方式上有着一些区别。本文将分别介绍SpringBoot和SpringMVC的特点和区别。一、SpringBoot的特点:简化配置:SpringBoot通过约定优于配置的原则,大大简化了项目的配置过程。它可以自动配置项目所需要的参数,开发人

springboot和springmvc有哪些区别 springboot和springmvc有哪些区别 Jun 07, 2023 am 10:10 AM

springboot和springmvc区别是:​1、含义不同;2、配置不同;3、依赖项不同;4、开发时间不同;5、生产力不同;6、实现JAR打包功能的方式不同;7、是否提供批处理功能;8、作用不同;9、社区和文档支持不同;10、是否需要部署描述符。

spring和springmvc有哪些区别 spring和springmvc有哪些区别 Dec 29, 2023 pm 05:02 PM

spring和springmvc的区别:1、定位和功能;2、核心功能;3、应用领域;4、扩展性。详细介绍:1、定位和功能,Spring是一个综合性的应用程序开发框架,提供了依赖注入、面向切面编程、事务管理等功能,旨在简化企业级应用程序的开发,而Spring MVC是Spring框架中的一个模块,用于Web应用程序的开发,实现了MVC模式;2、核心功能等等。

Java API 开发中使用 SpringMVC 进行 Web 服务处理 Java API 开发中使用 SpringMVC 进行 Web 服务处理 Jun 17, 2023 pm 11:38 PM

随着互联网的发展,Web服务越来越普遍。JavaAPI作为一种应用编程接口,也在不断地推出新的版本来适应不同的应用场景。而SpringMVC作为一种流行的开源框架,能够帮助我们轻松地构建Web应用程序。本文将详细讲解在JavaAPI开发中,如何使用SpringMVC进行Web服务处理,包括配置SpringMVC、编写控制器、使用

Java的SpringMVC拦截器怎么用 Java的SpringMVC拦截器怎么用 May 13, 2023 pm 02:55 PM

拦截器(interceptor)的作用SpringMVC的拦截器类似于Servlet开发中的过滤器Filter,用于对处理器进行预处理和后处理。将拦截器按一定的顺序联结成一条链,这条链称为拦截器链(InterceptorChain)。在访问被拦截的方法或字段时,拦截器链中的拦截器就会按其之前定义的顺序被调用。拦截器也是AOP思想的具体实现。拦截器和过滤器区别区别过滤器(Filter)拦截器(Intercepter)使用范围是servlet规范中的一部分,任何JavaWeb工程都可以使用是Spri

比较SpringBoot和SpringMVC的异同点 比较SpringBoot和SpringMVC的异同点 Dec 29, 2023 am 08:30 AM

解析SpringBoot和SpringMVC之间的异同SpringBoot和SpringMVC是Java领域中非常重要的开发框架。虽然它们都属于Spring框架的一部分,但是在使用和功能上有一些明显的区别。本文将对SpringBoot和SpringMVC进行比较,解析它们之间的异同。首先,让我们来了解一下SpringBoot。SpringBo

See all articles