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

在系列(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两个映射。运行测试,如下:

可以看到/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 ,结果如下:

可以看到已经正确的显示了我们请求的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查看结果:

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

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

可以看出映射的是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://localhost:8080/SpringMVCLesson/helloworld/reg/Hanmeimei-18查看结果:

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

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

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

这里可以看到结果映射到了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://localhost:8080/SpringMVCLesson/helloworld/paramstest查看结果:

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

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

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

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

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

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

可以看到又没有找到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://localhost:8080/SpringMVCLesson/helloworld/paramstest?example=BBB查看结果:

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

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

这次可以看到请求映射到了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=BBB查看结果:

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

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

可以看到没有找到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关系,即两个参数限制必须同时满足。

代码下载:http://pan.baidu.com/s/1i3j1Ttj

 
 
分类: Spring MVC
标签: SpringMVC

SpringMVCURL请求到Action的映射规则的更多相关文章

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

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

  2. SpringMVC(3)URL请求到Action的映射规则

    在SpringMVC(2)经典的HelloWorld实现我们展示了一个简单的get请求,并返回了一个简单的helloworld页面.本篇我们来学习如何来配置一个action的url映射规则. 在Spr ...

  3. SpringMVC 之URL请求到Action的映射(1)

    URL路径映射 1.1.对一个action配置多个URL映射: @RequestMapping(value={"/index", "/hello"}, meth ...

  4. Spring MVC 基于URL的映射规则(注解版)

    好几天没有跟进Spring MVC的学习了,之前看了点源码都忘的差不多了.这次就跟着之前的问题,继续总结下Spring MVC中的小知识. 关于SpringMVC的小demo可以参考这里! url-p ...

  5. SpringMVC注解汇总(二)-请求映射规则

    接上一节SpringMVC注解汇总-定义 讲到Httpy请求信息 URL路径映射 1)普通URL路径映射 @RequestMapping(value={"/test1", &quo ...

  6. @RequestMapping定义不同的处理器映射规则

    通过@RequestMapping注解可以定义不同的处理器映射规则. 1. URL路径映射 @RequestMapping(value="item")或@RequestMappin ...

  7. IIS rewrite映射规则语法格式

    IIS rewrite映射规则语法格式,特殊符号:&请用& amp;代替,否则异常. <configuration> <system.webServer> &l ...

  8. ASP.NET Web API是如何根据请求选择Action的?[下篇]

    ASP.NET Web API是如何根据请求选择Action的?[下篇] 再<上篇>中我们简单介绍了用于实现Action选择机制的HttpActionSelector,接下来我们来讨论本章 ...

  9. ASP.NET Web API是如何根据请求选择Action的?[上篇]

    ASP.NET Web API是如何根据请求选择Action的?[上篇] Web API的调用请求总是针对定义在某个HttpController中的某个Action方法,请求响应的内容来源于调用目标A ...

随机推荐

  1. jQuery性能优化篇

    jQuery高级技巧——性能优化篇 阅读目录 通过CDN(Content Delivery Network)引入jQuery库 减少DOM操作 适当使用原生JS 选择器优化 缓存jQuery对象 定义 ...

  2. HTTP Cookie深入理解

    HTTP Cookie 概述:Cookie通常也叫做网站cookie,浏览器cookie或者http cookie,是保存在用户浏览器端的,并在发出http请求时会默认携带的一段文本片段.它可以用来做 ...

  3. git stash用法

    使用场景: 当前修改的代码还不足以提交commit,但又必须切换到其他分支,要想完成这样的操作就可以使用git stash git stash意思就是备份当前的工作区的内容,从最近的一次提交中读取相关 ...

  4. ASP.NET MVC+EF框架+EasyUI实现权限管理系列(3)-面向接口的编程

    原文:ASP.NET MVC+EF框架+EasyUI实现权限管理系列(3)-面向接口的编程 ASP.NET MVC+EF框架+EasyUI实现权限管系列 (开篇)  (1)框架搭建    (2):数据 ...

  5. hdu-4419-Colourful Rectangle-段树区,并寻求

    这个问题很有趣的项目,写麻烦.它预计将有写了很长的时间. 好在,我想开了一个比较简单的方法.. . 使用位计算,颜色RGB分别1,2,4,代表. 状态的长度了. #include<stdio.h ...

  6. MVC 缓存1

    MVC 缓存 为什么要讲缓存.缓存到底有什么作用? 下面我们来说一个场景我们有一个首页菜单的布局基本是不会经常发生的变化,如果动态生成的 Web 页被频繁请求并且构建时需要耗用大量的系统资源,那么,如 ...

  7. hive union all 使用

    功能:将两个表中的 同样的字段拼接到一起 測试: create external table IF NOT EXISTS temp_uniontest_ta ( a1 string, a2 strin ...

  8. Mac在结构quick cocos2d-x编译环境

    关于 Quick 很多其它的使用说明可參考安装文件夹下的 README 文件. Quick-Coco2d-x开发工具 普通情况下,我们通常都会採用Cocos Code IDE作为开发工具来高速开发游戏 ...

  9. HDU 2070 Fibbonacci Number

    Fibbonacci Number Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others ...

  10. 虚拟WiFi

    转载用WinForm写一个虚拟WiFi助手玩玩(附源码)   这早不是什么新鲜的东西了,同类软件已经有很多,但不是收费就是有广告,在学校的时候就想自已写一个了,但那时候啥也没学,对C的掌握程度也就是定 ...