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的校驗參數,用以完善我們的註冊功能。

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