版权声明:本文为博主原创文章,博客地址:http://blog.csdn.net/a67474506?viewmode=contents

1.1. @RequestMapping映射请求

SpringMVC 使用 @RequestMapping 注解为控制器指定可以处理那些URL 请求

@requestMapping  可以定义在 类 和 方法 上

  1. package com.ibigsea.springmvc.helloworld;
  2. import org.springframework.stereotype.Controller;
  3. import org.springframework.web.bind.annotation.RequestMapping;
  4. @Controller
  5. public class HelloWorld {
  6. /**
  7. * 配置@RequestMapping 拦截 localhost:8080/springmvc/hello 请求
  8. * @return
  9. */
  10. @RequestMapping("/hello")
  11. public String helloWorld() {
  12. System.out.println("hello world");
  13. return "helloworld";
  14. }
  15. }
  1. package com.ibigsea.springmvc.helloworld;
  2. import org.springframework.stereotype.Controller;
  3. import org.springframework.web.bind.annotation.RequestMapping;
  4. @Controller
  5. @RequestMapping("/hello")
  6. public class HelloWorld {
  7. /**
  8. * 配置@RequestMapping 拦截 localhost:8080/springmvc/hello/world 请求
  9. * @return
  10. */
  11. @RequestMapping("/world")
  12. public String helloWorld(){
  13. System.out.println("hello world");
  14. return "helloworld";
  15. }
  16. }

@RequestMapping

– 类定义处:提供初步的请求映射信息。相对于 WEB 应用的根目录

– 方法处:提供进一步的细分映射信息。相对于类定义处的 URL。若

类定义处未标注 @RequestMapping,则方法处标记的 URL 相对于

WEB 应用的根目录

DispatcherServlet 截获请求后,就通过控制器上

@RequestMapping 提供的映射信息确定请求所对应的处理方法。

@RequestMapping 除了可以使用请求 URL 映射请求外,

还可以使用请求方法、请求参数及请求头映射请求

1.2. @RequestMapping限定请求方法、请求参数、请求头

  1. /**
  2. * 接收GET请求
  3. * @return
  4. */
  5. @RequestMapping(value="/get",method = RequestMethod.GET)
  6. public String get(){
  7. System.out.println("get");
  8. return "get";
  9. }
  10. /**
  11. * 接收POST请求
  12. * @return
  13. */
  14. @RequestMapping(value="/post",method = RequestMethod.POST)
  15. public String post(){
  16. System.out.println("post");
  17. return "post";
  18. }
  19. /**
  20. * 只接收 name 参数
  21. * @return
  22. */
  23. @RequestMapping(value="/params",params="name")
  24. public String params(String name){
  25. System.out.println("hello "+name);
  26. return "helloworld";
  27. }
  28. /**
  29. * 只接收请求头中 Content-Type 为 text/html;charset=UTF-8的请求
  30. * @return
  31. */
  32. @RequestMapping(value="/headers",headers="Content-Type:text/html;charset=UTF-8")
  33. public String headers(){
  34. System.out.println("headers");
  35. return "helloworld";
  36. }

1.3. @RequestMapping匹配符

– ?:匹配文件名中的一个字符

– *:匹配文件名中的任意字符

– **:** 匹配多层路径

实例:

URL : /user/*/create

-- /user/bigsea/create 、 /user/sea/create 等URL

URL : /user/**/create

-- /user/big/sea/create 、 /user/sea/big/create 等URL

URL : /user/create??

-- /user/createaa 、/user/createbb

1.4. @PathVariable 注解

带占位符的 URL 是 Spring3.0 新增的功能,该功能在SpringMVC 向 REST 目标挺进发展过程中具有里程碑的意义

通过 @PathVariable 可以将 URL 中占位符参数绑定到控制器处理方法的入参中:URL 中的 {xxx} 占位符可以通过@PathVariable("xxx") 绑定到操作方法的入参中。

  1. /**
  2. * localhost:8080/springmvc/hello/pathVariable/bigsea
  3. * localhost:8080/springmvc/hello/pathVariable/sea
  4. * 这些URL 都会 执行此方法 并且将  <b>bigsea</b>、<b>sea</b> 作为参数 传递到name字段
  5. * @param name
  6. * @return
  7. */
  8. @RequestMapping("/pathVariable/{name}")
  9. public String pathVariable(@PathVariable("name")String name){
  10. System.out.println("hello "+name);
  11. return "helloworld";
  12. }

JSP(这里指定全路径):

  1. <h1>pathVariable</h1>
  2. <a href="${pageContext.request.contextPath}/hello/pathVariable/bigsea" > name is bigsea </a>
  3. <br/>
  4. <a href="${pageContext.request.contextPath}/hello/pathVariable/sea" > name is sea</a>
  5. <br/>

运行结果:

  1. hello bigsea
  2. hello sea

1.5. @RequestParam 绑定请求参数

在处理方法入参处使用 @RequestParam 可以把请求参数传递给请求方法

– value:参数名

– required:是否必须。默认为 true, 表示请求参数中必须包含对应的参数,若不存在,将抛出异常

  1. /**
  2. * 如果 required = true 则表示请求参数对应的 字段 必须存在.如果不存在则会抛出异常<br/>
  3. * @param firstName 可以为null
  4. * @param lastName 不能为null .为null报异常
  5. * @param age age字段表示如果没有 age 参数 则默认值为 0
  6. * @return
  7. */
  8. @RequestMapping("/requestParam")
  9. public String requestParam(@RequestParam(value="firstName",required=false)String firstName,
  10. @RequestParam( value="lastName" ,required = true) String lastName,
  11. @RequestParam(value="age",required = false ,defaultValue="0")int age) {
  12. System.out.println("hello my name is " + (firstName == null ? "" : firstName)
  13. + lastName + "," + age +" years old this year");
  14. return "helloworld";
  15. }

Jsp:

  1. <a href="requestParam?firstName=big&lastName=sea" > name is bigsea , age is 0 </a>
  2. <br/>
  3. <a href="requestParam?lastName=sea&age=23" > name is sea , age is 23 </a>
  4. <br/>
  5. <a href="requestParam" > throws exception </a>

运行结果:

  1. hello my name is bigsea,0 years old this year
  2. hello my name is sea,23 years old this year

1.6. @RequestHeader 获取请求头

请求头包含了若干个属性,服务器可据此获知客户端的信息,通过 @RequestHeader 即可将求头中的属性值绑定到处理方法的入参中

  1. /**
  2. * 获取请求头中的信息
  3. * @RequestHeader 也有 value ,required ,defaultValue 三个参数
  4. * @param userAgent
  5. * @param cookie
  6. * @return
  7. */
  8. @RequestMapping("/requestHeader")
  9. public String requestHeader(@RequestHeader("User-Agent")String userAgent,@RequestHeader("Cookie")String cookie){
  10. System.out.println("userAgent:["+userAgent+"]");
  11. System.out.println("cookie:["+cookie+"]");
  12. return "helloworld";
  13. }

JSP:

  1. <a href="requestHeader" > requestHeader </a>

运行结果:

  1. userAgent:[Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/44.0.2383.0 Safari/537.36]
  2. cookie:[JSESSIONID=DA3B15F559349EA2C3F08BE772FCAFD8]

1.7. @CookieValue 获取 cookie值

  1. /**
  2. * 使用@CookieValue 绑定cookie值<br/>
  3. * 注解@CookieValue 也有 value ,required ,defaultValue 三个参数
  4. * @param session
  5. * @return
  6. */
  7. public String cookieValue(@CookieValue(value = "JSESSIONID", required= false)String session){
  8. System.out.println("JESSIONID:["+session+"]");
  9. return "helloworld";
  10. }

JSP:

  1. <a href="cookieValue" > cookieValue </a>

运行结果

  1. JESSIONID:[A4196EEDFD829B40CC1975F029A61328]

1.8. 源码分析

SpringMVC 学习笔记(二) @RequestMapping、@PathVariable等注解的更多相关文章

  1. SpringMVC 学习笔记(两) @RequestMapping、@PathVariable和其他注意事项

    1.1. @RequestMapping映射请求 SpringMVC 采用 @RequestMapping 注解为控制器指定能够处理那些URL 请求 @requestMapping  能够定义在 类  ...

  2. springMVC学习笔记(二)-----注解和非注解入门小程序

    最近一直在做一个电商的项目,周末加班,忙的都没有时间更新博客了.终于在上周五上线了,可以轻松几天了.闲话不扯淡了,继续谈谈springMvc的学习. 现在,用到SpringMvc的大部分使用全注解配置 ...

  3. SpringMVC学习笔记二:常用注解

    转载请注明原文地址:http://www.cnblogs.com/ygj0930/p/6831976.html  参考:http://www.cnblogs.com/leskang/p/5445698 ...

  4. SpringMVC:学习笔记(2)——RequestMapping及请求映射

    SpringMVC--RequestMapping及请求映射 @RequestMapping 说明 Spring MVC 使用 @RequestMapping 注解为控制器指定可以处理哪些 URL 请 ...

  5. springMVC学习笔记二

    六.springmvc的注解 xml的配置→注解 1.新建一个配置文件还是在config下 新建springAnnotation-servlet.xml web.xml 修改初始化为<param ...

  6. SpringMVC学习笔记(二)

    一.HandleMapping和HandlerAdapter的讲解 HandleMapping:处理映射器,可以理解为为请求的url查找对应的Controller类. HandlerAdapter:可 ...

  7. SpringMVC学习笔记六:使用 hibernate-validator注解式数据校验

    对客户端传过来的参数,在使用前一般需要进行校验. SpringMVC框架内置了Validator验证接口,但是实现起来太麻烦.我们一般使用 hibernate-validator进行数据校验. 1:j ...

  8. springmvc学习笔记二:重定向,拦截器,参数绑定

    Controller方法返回值 返回ModelAndView controller方法中定义ModelAndView对象并返回,对象中可添加model数据.指定view. 返回void 在Contro ...

  9. SpringMVC学习笔记二:参数接受

    该项目用来介绍SpringMVC对参数接受的方法: 项目目录树:在前一个项目上修改添加 新添加了Student类和Group类,用来测试整体参数接受 Student.java package com. ...

随机推荐

  1. css 水平、垂直居中

    水平居中 行内元素 行内元素:(img.span.文字等行内元素),通过在父级元素设置 text-align:center 使元素水平居中. 块级元素 块级元素:(div.p.h1...h6.ul.l ...

  2. inux命令英文缩写的含义(方便记忆) 2

    linux常用命令的英文单词缩写 命令缩写: ls:list(列出目录内容) cd:Change Directory(改变目录) su:switch user 切换用户rpm:redhat packa ...

  3. properties文件不能输入中文

    先把他关掉,然后对message.properties 文件右键--属性(properties), 右边最下面一行text file encoding选择other里面的最后一个utf-8, 再点击a ...

  4. [翻译]HLS实践

    最近公司项目没事做,课余实践研究一下技术,算是积累,也可以用到项目里,从零开始记录 HLS:Http Live Streaming 官方文档 https://developer.apple.com/s ...

  5. 寻找并输出11~999之间的回文数m

    寻找并输出11~999之间的数m,它满足m.m2和m3均为回文数. 回文:各位数字左右对称的整数. 例如:11满足上述条件 112=121,113=1331 判断一个数是否是回文数的方法:求该数的反序 ...

  6. 解决序列化类型为“System.Reflection.RuntimeModule”的对象时检测到循环引用。

    定义一个继承JavaScriptConverter的子类 public class DataTableConverter : JavaScriptConverter { /// <summary ...

  7. Good Bye 2014 E - New Year Domino 单调栈+倍增

    E - New Year Domino 思路:我用倍增写哒,离线可以不用倍增. #include<bits/stdc++.h> #define LL long long #define f ...

  8. 使用Nginx实现TCP反向代理

    Nginx 在1.9.0版本发布以前如果要想做到基于TCP的代理及负载均衡需要通过打名为 nginx_tcp_proxy_module 的第三方patch来实现,该模块的代码托管在github上网址: ...

  9. 洛谷P2471 [SCOI2007] 降雨量 [RMQ,模拟]

    题目传送门 降雨量 题目背景 07四川省选 题目描述 我们常常会说这样的话:“X年是自Y年以来降雨量最多的”.它的含义是X年的降雨量不超过Y年,且对于任意Y<Z<X,Z年的降雨量严格小于X ...

  10. google::proto::message.h

    整了一阵子google  proto message.h, 遇到很多问题,各种百度.google ,估计是用的人不是很多,整的焦头烂额,很多API都不知道该怎么用,只能一点一点的扣,为了方便在这里先简 ...