原文:http://www.cnblogs.com/liukemng/p/3726897.ht
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。
本人親測代碼:
@Controller @RequestMapping("/helloworld") public class HelloWorldContrloller { //一個action匹配多個URL路徑 @RequestMapping(value={"/index","/hello"},method=RequestMethod.GET) public String hello(String username,Model model){ System.out.println("//////////"+username); model.addAttribute("hello111", "Hello:"+username);//username匹配jsp頁input文本框的name值 return "success"; } //通配符 @RequestMapping(value="/detail/{id}",method=RequestMethod.GET) public String getDetail(@PathVariable(value="id") Integer id,Model model){ model.addAttribute("id", id); return "detail"; } }