本文簡介
在前一篇文章中給大家講述了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中的佔位符參數版頂到控制器處理的方法入參當中。
- @RequestMapping("/user/{userId}")
- public ModelAndView showUser(@PathVariable("userId") String userId) {
- ModelAndView mv = new ModelAndView();
- .......
- return mv;
- }
@RequestMapping除了可以使用請求URL映射請求,還可以使用請求方法、請求頭參數及請求參數映射請求。
- @RequestMapping(value = "/delete", method = RequestMethod.POST, params = "userId")
- @ResponseBody
- public String showUser(@RequestParam("userId") String userId) {
- return "";
- }
- @RequestMapping(value = "/delete", headers = "content-type=text/*")
- @ResponseBody
- public String showUser(@RequestParam("userId") String userId) {
- return "";
- }
@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可以綁定些什麼。
- // 與請求有關的
-
- /**
- * 綁定HttpServletRequest、java.security.Principal、java.util.Locale
- */
- @RequestMapping(value = "/data/standard/request", method = RequestMethod.GET)
- public @ResponseBody
- String standardRequestArgs(HttpServletRequest request, Principal user,
- Locale locale) {
- StringBuilder buffer = new StringBuilder();
- buffer.append("request = ").append(request).append(", ");
- buffer.append("userPrincipal = ").append(user).append(", ");
- buffer.append("requestLocale = ").append(locale);
- return buffer.toString();
- }
-
- /**
- * 綁定Reader 通過Servlet的ServletRequest獲得。
- */
- @RequestMapping(value = "/data/standard/request/reader", method = RequestMethod.POST)
- public @ResponseBody
- String requestReader(Reader requestBodyReader) throws IOException {
- return "Read char request body = "
- + FileCopyUtils.copyToString(requestBodyReader);
- }
-
- /**
- * 綁定InputStream 通過Servlet的ServletRequest獲得。
- */
- @RequestMapping(value = "/data/standard/request/is", method = RequestMethod.POST)
- public @ResponseBody
- String requestReader(InputStream requestBodyIs) throws IOException {
- return "Read binary request body = "
- + new String(FileCopyUtils.copyToByteArray(requestBodyIs));
- }
-
- // 響應有關的
- /**
- * 綁定HttpServletResponse
- */
- @RequestMapping("/data/standard/response")
- public @ResponseBody
- String response(HttpServletResponse response) {
- return "response = " + response;
- }
-
- /**
- * 綁定Writer 通過Servlet的ServletResponse獲得。
- */
- @RequestMapping("/data/standard/response/writer")
- public void availableStandardResponseArguments(Writer responseWriter)
- throws IOException {
- responseWriter.write("Wrote char response using Writer");
- }
-
- /**
- * 綁定OutputStream 通過Servlet的ServletResponse獲得。
- */
- @RequestMapping("/data/standard/response/os")
- public void availableStandardResponseArguments(OutputStream os)
- throws IOException {
- os.write("Wrote binary response using OutputStream".getBytes());
- }
-
- // HttpSession
- /**
- * 綁定HttpSession
- */
- @RequestMapping("/data/standard/session")
- public @ResponseBody
- String session(HttpSession session) {
- StringBuilder buffer = new StringBuilder();
- buffer.append("session=").append(session);
- return buffer.toString();
- }
結束語
在本節大家已經大致瞭解了SpringMVC可以使用哪些映射方式,和可以綁定哪些參數。相信這種靈活的請求映射和靈活的參數綁定會給大家帶來很多方便。
下一講我們將學習SpringMVC的校驗參數,用以完善我們的註冊功能。