1.Handler的理解?
?一個handler就是一個控制器里的某個方法,而通常情況下,該方法會對應到相應的url。
2.每個Handler的返回值?
?1)返回的是ModelAndView對象?
ModelAndView代表的是響應的視圖,還有一個向該視圖傳遞的數(shù)據(jù)。比如:
@RequestMapping(value="/getalluser.action")
?? publicModelAndView getAllUser(){
???????? ModelAndViewmodel = new ModelAndView();
???????? Listlist = userInfoService.getList(null);
?model.addObject("users",list);//表示向頁面?zhèn)鬟f的數(shù)據(jù)
???????? model.setViewName("/index.jsp");//表示呈現(xiàn)的頁面
???????? returnmodel;
?? }
2)返回的是String類型?
?? a.轉(zhuǎn)發(fā)。類似于request.getRequestDispacher(“頁面名稱”).forward(request,response);
???? 特點:url路徑不發(fā)生改變,但是request共享。
在handler方法返回的時候:return?“forward:/edit.action”;
?? b.重定向,類似于response.sendRedirect(“頁面名稱”);
???? 特點:url路徑發(fā)生改變,但是request不共享。
在handler中返回值:return?"redirect:/item.action";
3)返回的是void類型。有的handler的處理是不需要有返回頁面的情況,那這個時候就采取返回的類型為void。
?? 適用情況:請求json或者xml的數(shù)據(jù)格式,或者請求的就是一串字符串數(shù)據(jù)。
??? 寫法:類似于servlet的處理。
@RequestMapping(value=”/aaa.action”)
public?voidaddType(HttpServletRequest req,HttpServletResponse resp){
???????//JSON處理
?????? ?List list =userService.getList();
?//序列化的操作
?JSONArray?jsonArray =?JSONArray.fromObject(list);
???????? ?? String strInfo = ? jsonArray.toString();
System.out.println(strInfo);
???????? ?? PrintWriter pw = response.getWriter();
???????? ?? pw.print(strInfo);
??? }
2.URL映射
1)同一個handler處理多個路徑的情況下:
@RequestMapping(value={"/index","/hello"}, method = {RequestMethod.GET})
以上表示的就是可以處理index.action和hello.action的路徑。
method表示限定的請求方法,
2)url風格設定?
?? restful風格設定:
我們的請求路徑的風格為這樣:
?? /方法名/參數(shù)1/參數(shù)2
?? 案例:
@RequestMapping(value="/detail/{id}",method={RequestMethod.GET})
????? public ModelAndViewdetail(@PathVariable(value="id") Integer id){
?????????? System.out.println("hello");
?????????? ModelAndView model = newModelAndView();
????? ???UserInfo u = userInfoService.getUserInfo(id);
????? ???model.addObject("user", u);
????? ???model.setViewName("/user.jsp");
?????????? return model;
????? }
注意:{}里的參數(shù)名和PathVariable里的value里的名字一定要一致。
訪問路徑:http://localhost:10086/ssmdemo2/detail/81.action
3)通配符映射:
???????????????我們還可以通過通配符對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”;
如果現(xiàn)在有“/helloworld/index”和“/helloworld/*”,如果請求地址為“/helloworld/index”那么將如何匹配?Spring MVC會按照最長匹配優(yōu)先原則(即和映射配置中哪個匹配的最多)來匹配,所以會匹配“/helloworld/index
4)url正則表達式匹配
Spring MVC還支持正則表達式方式的映射配置,我們通過一
@RequestMapping(value="/reg/{name:\\w+}-{age:\\d+}",method = {RequestMethod.GET})
publicModelAndView regUrlTest(@PathVariable(value="name") String name,@PathVariable(value="age") Integer age){
?? }