SpringMVC 第三篇【灵活多变的方法映射】

本文简介

       在前一篇文章中给大家讲述了SpringMVC的大致原理,希望通过这个引子能让大家以后遇到问题之后能从原理上找出问题所在帮助大家解决一些问题。在本篇文章中,本人将和大家一起学习SpringMVC十分灵活的方法映射。

      使用@ReeuestMapping映射请求

     @RequestMapping使用value值来指定请求的URL,如@RequestMapping("/home")、@RequestMapping("/index")等等。(注意:@RequestMapping在类定义出指定的URL相对于Web应用的部署路径,而在方法出和定初一指定的URL是相对于类定义处指定的URL  这点上篇文章已经给出解释。)

     @RequestMapping不但支持标准的URL还支持 Ant风格(?、*和**的字符 )和带{xxx}占位符的URL。如:

     /user/*/createUser:匹配/user/adfaf/createUser、/user/12/createUser等URL

     /user/**/createUser:匹配/user/createUser、/user/affa/createUser等URL

     /user/createUser??:匹配/user/createUseraa、/user/createUserbb等URL

     /user/{userId}:匹配/user/12241241、/user/211221等URL

     /user/**/{userId}:匹配/user/afaf/afds/1241、/user/af/32等URL

     其中带占位符的URL是Spring3.0新增的功能。通过@PathVariable可以将URL中的占位符参数版顶到控制器处理的方法入参当中。

  1. @RequestMapping("/user/{userId}")
  2. public ModelAndView showUser(@PathVariable("userId") String userId) {
  3. ModelAndView mv = new ModelAndView();
  4. .......
  5. return mv;
  6. }

 @RequestMapping除了可以使用请求URL映射请求,还可以使用请求方法、请求头参数及请求参数映射请求。

  1. @RequestMapping(value = "/delete", method = RequestMethod.POST, params = "userId")
  2. @ResponseBody
  3. public String showUser(@RequestParam("userId") String userId) {
  4. return "";
  5. }
  1.  
  1. @RequestMapping(value = "/delete", headers = "content-type=text/*")
  2. @ResponseBody
  3. public String showUser(@RequestParam("userId") String userId) {
  4. return "";
  5. }

 @RequestMapping的value、method、params及headers分别表示请求URL、请求方法、请求参数及报文头的映射条件。它们之间是与的关系,联合使用可以让请求映射更加精确化。

params和headers分别通过请求参数和报文头属性进行映射,它们支持简单的表达式,下面以params表达式为例说明,headers可以参照params进行理解。

"param1":表示请求必须包含名为param1的请求参数

"!param1":表示请求不能包含名为param1的请求参数

"param1!=value1":表示请求参数包含param1的参数 但是其值不能为value1

{"param1=value1","param2"}:表示请求参数必须包含param1、param2两个参数  并且param1的值必须为value1.

 

SpringMVC绑定入参

     SpringMVC支持绑定多种入参,方便大家的使用。看看下面的代码,看看Spring可以绑定些什么。

  1. // 与请求有关的
  2.  
  3. /**
  4. * 绑定HttpServletRequest、java.security.Principal、java.util.Locale
  5. */
  6. @RequestMapping(value = "/data/standard/request", method = RequestMethod.GET)
  7. public @ResponseBody
  8. String standardRequestArgs(HttpServletRequest request, Principal user,
  9. Locale locale) {
  10. StringBuilder buffer = new StringBuilder();
  11. buffer.append("request = ").append(request).append(", ");
  12. buffer.append("userPrincipal = ").append(user).append(", ");
  13. buffer.append("requestLocale = ").append(locale);
  14. return buffer.toString();
  15. }
  16.  
  17. /**
  18. * 绑定Reader 通过Servlet的ServletRequest获得。
  19. */
  20. @RequestMapping(value = "/data/standard/request/reader", method = RequestMethod.POST)
  21. public @ResponseBody
  22. String requestReader(Reader requestBodyReader) throws IOException {
  23. return "Read char request body = "
  24. + FileCopyUtils.copyToString(requestBodyReader);
  25. }
  26.  
  27. /**
  28. * 绑定InputStream 通过Servlet的ServletRequest获得。
  29. */
  30. @RequestMapping(value = "/data/standard/request/is", method = RequestMethod.POST)
  31. public @ResponseBody
  32. String requestReader(InputStream requestBodyIs) throws IOException {
  33. return "Read binary request body = "
  34. + new String(FileCopyUtils.copyToByteArray(requestBodyIs));
  35. }
  36.  
  37. // 响应有关的
  38. /**
  39. * 绑定HttpServletResponse
  40. */
  41. @RequestMapping("/data/standard/response")
  42. public @ResponseBody
  43. String response(HttpServletResponse response) {
  44. return "response = " + response;
  45. }
  46.  
  47. /**
  48. * 绑定Writer 通过Servlet的ServletResponse获得。
  49. */
  50. @RequestMapping("/data/standard/response/writer")
  51. public void availableStandardResponseArguments(Writer responseWriter)
  52. throws IOException {
  53. responseWriter.write("Wrote char response using Writer");
  54. }
  55.  
  56. /**
  57. * 绑定OutputStream 通过Servlet的ServletResponse获得。
  58. */
  59. @RequestMapping("/data/standard/response/os")
  60. public void availableStandardResponseArguments(OutputStream os)
  61. throws IOException {
  62. os.write("Wrote binary response using OutputStream".getBytes());
  63. }
  64.  
  65. // HttpSession
  66. /**
  67. * 绑定HttpSession
  68. */
  69. @RequestMapping("/data/standard/session")
  70. public @ResponseBody
  71. String session(HttpSession session) {
  72. StringBuilder buffer = new StringBuilder();
  73. buffer.append("session=").append(session);
  74. return buffer.toString();
  75. }

 

结束语

      在本节大家已经大致了解了SpringMVC可以使用哪些映射方式,和可以绑定哪些参数。相信这种灵活的请求映射和灵活的参数绑定会给大家带来很多方便。

      下一讲我们将学习SpringMVC的校验参数,用以完善我们的注册功能。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章