javascript
SpringMVC学习之用户登录(二)
要點(diǎn):參數(shù)傳遞和接受的各種方式
參考資料:http://lydia-fly.iteye.com/blog/2152073
參考資料:http://blog.csdn.net/z69183787/article/details/41653843
Spring接收請求參數(shù):
?
1,使用HttpServletRequest獲取
Java代碼???2,Spring會(huì)自動(dòng)將表單參數(shù)注入到方法參數(shù),和表單的name屬性保持一致。和Struts2一樣
Java代碼???
3,自動(dòng)注入Bean屬性
??
Java代碼???
Java代碼???
向頁面?zhèn)髦?#xff1a;
當(dāng)Controller組件處理后,向jsp頁面?zhèn)髦?#xff0c;
1,使用HttpServletRequest 和 Session ?然后setAttribute(),就和Servlet中一樣
2,使用ModelAndView對象
3,使用ModelMap對象
4,使用@ModelAttribute注解
?
Model數(shù)據(jù)會(huì)利用HttpServletRequest的Attribute傳值到success.jsp中
Java代碼???
使用ModelMap參數(shù)對象示例:
ModelMap數(shù)據(jù)會(huì)利用HttpServletRequest的Attribute傳值到success.jsp中
Java代碼???
?使用@ModelAttribute示例
在Controller方法的參數(shù)部分或Bean屬性方法上使用
@ModelAttribute數(shù)據(jù)會(huì)利用HttpServletRequest的Attribute傳值到success.jsp中
Java代碼???
Session存儲(chǔ):
可以利用HttpServletReequest的getSession()方法
Java代碼???
Spring MVC 默認(rèn)采用的是轉(zhuǎn)發(fā)來定位視圖,如果要使用重定向,可以如下操作
1,使用RedirectView
2,使用redirect:前綴
Java代碼??? ?或者用如下方法,工作中常用的方法:
Java代碼???
?
首先貼出Controller的全部內(nèi)容
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 | /** ?*?@author?<a?href="http://www.xdemo.org">xdemo.org</a> ?*/ @Controller @RequestMapping(value="/request") public?class?RequestParamController?{ ???????????????? ???????????????/** ????????????????*?最簡的配置,不是用@RequestParam,效果和get2一樣,默認(rèn)required=false ????????????????*?請求方式不限 ????????????????*?@param?p1 ????????????????*?@param?map ????????????????*/ ???????????????@RequestMapping(value="get0") ???????????????public?void?get0(String?p1,ModelMap?map){ ???????????????????????????????map.addAttribute("p1",?p1);//往頁面?zhèn)鬟f ???????????????} ???????????????? ???????????????/** ????????????????*?value="p1"表示參數(shù)名稱<br> ????????????????*?required=true表示如果沒有傳遞參數(shù)"p1",則會(huì)報(bào)400參數(shù)異常<br> ????????????????*?使用void表示約定的路徑,即request/get1.jsp ????????????????*?@param?p1 ????????????????*?@param?map ????????????????*/ ???????????????@RequestMapping(value="get1",method=RequestMethod.GET) ???????????????public?void?get1(@RequestParam(value="p1",required=true)String?p1,ModelMap?map){ ???????????????????????????????map.addAttribute("p1",?p1);//往頁面?zhèn)鬟f ???????????????} ???????????????? ???????????????/** ????????????????*?和get1不同的是,p1這個(gè)參數(shù)不一定非得需要,即使不給這個(gè)參數(shù),也可以正常運(yùn)行<br> ????????????????*?返回String是視圖的名稱,只要將map賦值,給的值也會(huì)帶到前抬 ????????????????*?@param?p1 ????????????????*?@param?map ????????????????*?@return ????????????????*/ ???????????????@RequestMapping(value="get2",method=RequestMethod.GET) ???????????????public?String?get2(@RequestParam(value="p1",required=false)String?p1,ModelMap?map){ ???????????????????????????????map.addAttribute("p1",?p1);//往頁面?zhèn)鬟f ???????????????????????????????return?"request/get2"; ???????????????} ???????????????? ???????????????/** ????????????????*?和get2不同的是,返回的對象是ModelAndView ????????????????*?表示綁定了視圖和數(shù)據(jù)的對象,數(shù)據(jù)就是ModelMap中的Key-Value ????????????????*?@param?p1 ????????????????*?@param?map ????????????????*?@return ????????????????*/ ???????????????@RequestMapping(value="get3",method=RequestMethod.GET) ???????????????public?ModelAndView?get3(@RequestParam(value="p1",required=false)String?p1,ModelMap?map){ ???????????????????????????????map.addAttribute("p1",?p1); ???????????????????????????????return?new?ModelAndView("request/get2",map); ???????????????} ???????????????? ???????????????/** ????????????????*?跳轉(zhuǎn)到頁面 ????????????????*?@throws?NoSuchAlgorithmException? ????????????????*/ ???????????????@RequestMapping("userForm") ???????????????public?String?userForm(HttpServletResponse?response)?throws?NoSuchAlgorithmException{ ???????????????????????????????CookieUtils.writeCookie(response,?-1,?"x",?"dddddddddddddd"); ???????????????????????????????return?"request/userForm"; ???????????????} ???????????????? ???????????????/** ????????????????*?綁定數(shù)據(jù)到User對象,支持Map,Set,List,Array等,但是需要使用下標(biāo),不是很靈活 ????????????????*?請查看user2的寫法 ????????????????*?@param?user ????????????????*?@param?map ????????????????*/ ???????????????@RequestMapping(value="user") ???????????????public?void?user(User?user,ModelMap?map){ ???????????????????????????????map.addAttribute("user",?user); ???????????????} ???????????????? ???????????????/** ????????????????*?這里可以接受List,Array,Set等,寫法是一樣的,注意前端寫法<br> ????????????????*?另外這個(gè)必須要使用MappingJacksonHttpMessageConverter這個(gè)消息轉(zhuǎn)換器 ????????????????*?請看我上面的配置 ????????????????*?@param?user ????????????????*?@return ????????????????*/ ???????????????@ResponseBody ???????????????@RequestMapping("user2") ???????????????public?String?user2(@RequestBody?List<User>?user){ ???????????????????????????????System.out.println(user.size()); ???????????????????????????????return?""; ???????????????} ???????????????? ???????????????/** ????????????????*?這個(gè)方法只支持POST ????????????????*?@param?s ????????????????*?@return ????????????????*/ ???????????????@ResponseBody ???????????????@RequestMapping("array") ???????????????public?String?array(@RequestBody?String[]?s){ ???????????????????????????????System.out.println(s.length); ???????????????????????????????return?""; ???????????????} ???????????????? ???????????????/** ????????????????*?這個(gè)比較奇葩,來自一位朋友的寫法,即.xxx/5,4這樣的請求,SpringMVC竟然也是支持的 ????????????????*?@param?id ????????????????*?@return ????????????????*/ ???????????????@ResponseBody ???????????????@RequestMapping(value="array/{id}",method=RequestMethod.GET) ???????????????public?String?array2(@PathVariable("id")Long[]?id){ ???????????????????????????????System.out.println(id.length); ???????????????????????????????return?"array?length:"+id.length+""; ???????????????} ???????????????? ???????????????/** ????????????????*?一個(gè)表單對應(yīng)多個(gè)Bean對象,這些Bean中有相同的屬性,那么需要在分裝他們的一個(gè)整體的對象 ????????????????*?使之支持object.property的表達(dá)式 ????????????????*?@param?c ????????????????*/ ???????????????@ResponseBody ???????????????@RequestMapping("complex") ???????????????public?void?complexObject(C?c){ ???????????????????????????????System.out.println(c.getA().getX()); ???????????????????????????????System.out.println(c.getB().getX()); ???????????????????????????????? ???????????????} ???????????????? ???????????????/** ????????????????*?讀取Cookie的值 ????????????????*?@param?x ????????????????*?@return ????????????????*/ ???????????????@ResponseBody ???????????????@RequestMapping("cookie") ???????????????public?String?cookie(@CookieValue("x")String?x){ ???????????????????????????????return?x; ???????????????} ???????????????? } |
這種方式支持get和post,參數(shù)可選
| 1 2 3 4 5 6 7 8 9 10 | /** *?最簡的配置,不是用@RequestParam,效果和get2一樣,默認(rèn)required=false *?請求方式不限 *?@param?p1 *?@param?map */ @RequestMapping(value="get0") public?void?get0(String?p1,ModelMap?map){ ???????map.addAttribute("p1",?p1);//往頁面?zhèn)鬟f } |
訪問方式簡單的比如http://localhost:8080/springmvc-param/request/get0?p1=xxx。
?這種方式支持get,參數(shù)必須
| 1 2 3 4 5 6 7 8 9 10 11 | /** *?value="p1"表示參數(shù)名稱<br> *?required=true表示如果沒有傳遞參數(shù)"p1",則會(huì)報(bào)400參數(shù)異常<br> *?使用void表示約定的路徑,即request/get1.jsp *?@param?p1 *?@param?map */ @RequestMapping(value="get1",method=RequestMethod.GET) public?void?get1(@RequestParam(value="p1",required=true)String?p1,ModelMap?map){ ???????map.addAttribute("p1",?p1);//往頁面?zhèn)鬟f } |
這種方式和第一種不同的是,指定了訪問訪問必須為GET,而且參數(shù)是必須的,可以通過如下方式訪問這個(gè)地址:http://localhost:8080/springmvc-param/request/get1?p1=xxxx。
這種方式僅支持GET,參數(shù)可選
| 1 2 3 4 5 6 7 8 9 10 11 12 | /** *?和get1不同的是,p1這個(gè)參數(shù)不一定非得需要,即使不給這個(gè)參數(shù),也可以正常運(yùn)行<br> *?返回String是視圖的名稱,只要將map賦值,給的值也會(huì)帶到前抬 *?@param?p1 *?@param?map *?@return */ @RequestMapping(value="get2",method=RequestMethod.GET) public?String?get2(@RequestParam(value="p1",required=false)String?p1,ModelMap?map){ ???????map.addAttribute("p1",?p1);//往頁面?zhèn)鬟f ???????return?"request/get2"; } |
這個(gè)方法和第二種唯一不同的就是參數(shù)是可選的,其他沒有不同。
?這種方式僅支持GET,參數(shù)可選
| 1 2 3 4 5 6 7 8 9 10 11 12 | /** *?和get2不同的是,返回的對象是ModelAndView *?表示綁定了視圖和數(shù)據(jù)的對象,數(shù)據(jù)就是ModelMap中的Key-Value *?@param?p1 *?@param?map *?@return */ @RequestMapping(value="get3",method=RequestMethod.GET) public?ModelAndView?get3(@RequestParam(value="p1",required=false)String?p1,ModelMap?map){ ???????map.addAttribute("p1",?p1);//往頁面?zhèn)鬟f ???????return?new?ModelAndView("request/get2",map); } |
ModelAndView表示綁定了數(shù)據(jù)的視圖,可以通過EL表達(dá)式去取值。
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | /** ?*?跳轉(zhuǎn)到頁面 ?*?@throws?NoSuchAlgorithmException? ?*/ @RequestMapping("userForm") public?String?userForm(HttpServletResponse?response)?throws?NoSuchAlgorithmException{ ???????CookieUtils.writeCookie(response,?-1,?"x",?"dddddddddddddd"); ???????return?"request/userForm"; } /** *?讀取Cookie的值 *?@param?x *?@return */ @ResponseBody @RequestMapping("cookie") public?String?cookie(@CookieValue("x")String?x){ ???????return?x; } |
先訪問http://localhost:8080/springmvc-param/request/userForm這個(gè)方法,跳轉(zhuǎn)到一個(gè)頁面,并向?yàn)g覽器寫入Cookie,第二個(gè)方法訪問的時(shí)候即可通過@CookieValue方式來取到Cookie中的值。
綁定數(shù)據(jù)到一個(gè)對象上,支持get和post
一個(gè)User,一個(gè)Phone,一個(gè)User擁有多個(gè)Phone,為了演示,User中有一個(gè)List和Array的Phone的集合
| 1 2 3 4 5 6 7 8 | public?class?User?{ ???????????????? ???????private?String?userName; ???????private?String?address; ???????private?List<Phone>?phones; ???????private?Phone[]?phones2; ???????//省略GET和SET... } |
| 1 2 3 | public?class?Phone?{ ???????????????private?String?brand;//手機(jī)品牌 } |
Controller方法如下
| 1 2 3 4 5 6 7 8 9 10 | /** *?綁定數(shù)據(jù)到User對象,支持Map,Set,List,Array等,但是需要使用下標(biāo),不是很靈活 *?請查看user2的寫法 *?@param?user *?@param?map */ @RequestMapping(value="user") public?void?user(User?user,ModelMap?map){ ???????map.addAttribute("user",?user); } |
HTML表單如下
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 | <form?action="request/user"?method="get"?style="border:1px?solid?red;"> ?????<table> ???????????<tr><td?colspan="2">這個(gè)表單演示了對象數(shù)據(jù)綁定的方法,以及對象中的Set,List,Array數(shù)據(jù)綁定(三者類似)</td></tr> ????????????<tr> ????????????????<td>用戶名:</td> ????????????????<td><input?type="text"?name="userName"?value="張三"></td> ????????????</tr> ????????????<tr> ????????????????<td>用戶地址:</td> ????????????????<td><input?type="text"?name="address"?value="江蘇省無錫市新區(qū)菱湖大道200號"><br></td> ????????????</tr> ????????????<tr> ????????????????<td>手機(jī)品牌:</td> ????????????????<td> ????????????????????<input?type="text"?name="phones[0].brand"?value="SONY"><br> ????????????????????<input?type="text"?name="phones[1].brand"?value="MOTO"><br> ????????????????????<input?type="text"?name="phones[2].brand"?value="LG"><br> ?????????????????</td> ?????????????</tr> ?????????????<tr> ?????????????????<td>手機(jī)品牌2:</td> ?????????????????<td> ?????????????????????<input?type="text"?name="phones2[0].brand"?value="Apple"><br> ?????????????????????<input?type="text"?name="phones2[1].brand"?value="Samsung"><br> ?????????????????????<input?type="text"?name="phones2[2].brand"?value="HTC"><br> ?????????????????</td> ??????????????</tr> ??????????????<tr> ??????????????????<td?colspan="2"?style="text-align:?right;"> ??????????????????<input?type="submit"?value="提交"> ??????????????????</td> ???????????????</tr> ???????</table> </form> |
一對多的時(shí)候,使用多一方的在一一方的對象中的屬性名,加上數(shù)組下標(biāo),即phones[0].brand,phones[1].brand即可綁定到User的phones屬性上,這種方法的局限性就是要求下標(biāo)是正確的,否則會(huì)無法綁定,不是很方便,但是也有其適用場景。
下面這種方法就是比較方便了,僅支持post,但是必須要在消息轉(zhuǎn)換器中配置JSON解析器
| 1 | <bean?class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter"></bean> |
并注冊到RequestMappingHandlerAdapter的messageConverters中。
Controller如下:
| 1 2 3 4 5 6 7 8 9 10 11 12 13 | /** ?*?這里可以接受List,Array,Set等,寫法是一樣的,注意前端寫法<br> ?*?另外這個(gè)必須要使用MappingJacksonHttpMessageConverter這個(gè)消息轉(zhuǎn)換器 ?*?請看我上面的配置 ?*?@param?user ?*?@return ?*/ ?@ResponseBody ?@RequestMapping("user2") ?public?String?user2(@RequestBody?List<User>?user){ ?????????System.out.println(user.size()); ?????????return?""; ?} |
JavaScript如下
| 1 2 3 4 5 6 7 8 9 10 11 12 13 | var?userList=?new?Array(); userList.push({userName:"xx",address:"fff"}); userList.push({userName:"zzzz",address:"ggggg"}); $.ajax({ ??url:"request/user2", ??type:"post", ??data:JSON.stringify(userList), ??dataType:"json", ??contentType:"application/json", ??success:function(data){ ???},error:function(data){ ??} }); |
該方法僅支持POST的方式,會(huì)使用到json2.js這個(gè)類庫,注意設(shè)置contentType:"application/json"這個(gè)屬性,否則會(huì)報(bào)415未知的類型異常。
傳遞簡單的字符串?dāng)?shù)組,僅支持POST方式
| 1 2 3 4 5 6 7 8 9 10 11 12 | /** *?傳遞簡單的字符串?dāng)?shù)組 *?這個(gè)方法只支持POST *?@param?s *?@return */ @ResponseBody @RequestMapping("array") public?String?array(@RequestBody?String[]?s){ ????System.out.println(s.length); ????return?""; ?} |
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | var?array=new?Array(); array.push(1); array.push(2); array.push(3); array.push(4); array.push(5); $.ajax({ ????url:"request/array", ????type:"post", ????dataType:"json", ????data:JSON.stringify(array), ????dataType:"json", ????contentType:"application/json", ????success:function(data){ ????},error:function(data){ ????} }); |
和上面的方法類似,注意contentType:"application/json",否則同樣的415錯(cuò)誤。
下面的方法是restful中的路徑變量,支持get,post,delete等,如:xxx/1,xxx/2這種方式,經(jīng)測試,這個(gè)方法的奇葩之處在于"xxx/5,4"以及"xxx/[5,4]"的效果是一樣的,看代碼:
| 1 2 3 4 5 6 7 8 9 10 11 | /** *?這個(gè)比較奇葩,來自一位朋友的寫法,即.xxx/5,4這樣的請求,SpringMVC竟然也是支持的 *?@param?id *?@return */ @ResponseBody @RequestMapping(value="array/{id}",method=RequestMethod.GET) public?String?array2(@PathVariable("id")Long[]?id){ ???????System.out.println(id.length); ???????return?"array?length:"+id.length+""; } |
可以直接將后面的路徑變量,轉(zhuǎn)換成相應(yīng)的數(shù)組。可以在瀏覽器輸入:http://localhost:8080/springmvc-param/request/array/5,4,3,2,1或者h(yuǎn)ttp://localhost:8080/springmvc-param/request/array/[5,4,3,2,1],都可以轉(zhuǎn)換成數(shù)組。
如果一個(gè)表單對應(yīng)多個(gè)實(shí)體類,恰好這些類中具有相同的屬性,這時(shí)候SpringMVC就犯難了,我們要做的是讓SpringMVC明白我們在給誰賦值。
支持post,get,put
如下,A,B,C,其中C中包含了A和B兩個(gè)成員變量
| 1 2 3 | public?class?A?{ ???????private?String?x; } |
| 1 2 3 | public?class?B?{ ???????private?String?x; } |
| 1 2 3 4 | public?class?C?{ ???????private?A?a; ???????private?B?b; } |
Controller如下
| 1 2 3 4 5 6 7 8 9 10 11 | /** *?一個(gè)表單對應(yīng)多個(gè)Bean對象,這些Bean中有相同的屬性,那么需要在分裝他們的一個(gè)整體的對象 *?使之支持object.property的表達(dá)式 *?@param?c */ @ResponseBody @RequestMapping("complex") public?void?complexObject(C?c){ ???????System.out.println(c.getA().getX()); ???????System.out.println(c.getB().getX()); } |
HTML如下:
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | <form?action="request/complex"?method="POST"?style="border:1px?solid?red;"> ??????<table> ?????????????<tr> ?????????????????<td>A對象:</td> ?????????????????<td><input?type="text"?name="a.x"?value="xxx"></td> ??????????????</tr> ??????????????<tr> ??????????????????<td>B對象:</td> ??????????????????<td><input?type="text"?name="b.x"?value="yyy"><br></td> ??????????????</tr> ??????????????<tr> ??????????????????<td?colspan="2"?style="text-align:?right;"> ?????????????????????<input?type="submit"?value="提交"> ??????????????????</td> ??????????????</tr> ????????</table> </form> |
通過object.property即可指定給誰賦值。
另外一個(gè)是關(guān)于Session取值的
代碼如下
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | @Controller @SessionAttributes(value="user") @RequestMapping("/session") public?class?SessionController?{ ????@RequestMapping(method=RequestMethod.GET)????? ????public?String?setUser(ModelMap?map){? ????????User?user=new?User();? ????????user.setAddress("xxx");? ????????user.setUserName("yyy"); ????????map.put("user",?user); ????????return?"request/userForm"; ????} ???????????????? ????@ResponseBody ????@RequestMapping(value="getUser",method=RequestMethod.GET) ????public?String?getUser(@ModelAttribute("user")User?user){ ???????????System.out.println(user.getUserName()); ???????????return?user.getUserName(); ????} } |
在Controller上加上注解@SessionAttributes(value="user"),再使用ModelMap的put方法(非addAttribute方法),然后在getUser方法中,使用@ModelAttribute("user")即可取得session中的user對象
Maven依賴:
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 | <properties> ????????????<springframework>4.0.5.RELEASE</springframework> ????????????<servlet>3.1.0</servlet> ????????????<jstl>1.2</jstl> ????????????<xstream>1.4.7</xstream> ????????????<commons-fileupload>1.3.1</commons-fileupload> ????????????<jackson>1.9.13</jackson> </properties> <dependencies> ????????????<!--?jackson?json解析支持?--> ????????????<dependency> ????????????????????????<groupId>org.codehaus.jackson</groupId> ????????????????????????<artifactId>jackson-mapper-asl</artifactId> ????????????????????????<version>${jackson}</version> ????????????</dependency> ????????????<!--?Spring?web?mvc?--> ????????????<dependency> ????????????????????????<groupId>org.springframework</groupId> ????????????????????????<artifactId>spring-webmvc</artifactId> ????????????????????????<version>${springframework}</version> ????????????</dependency> ????????????<!--?servlet?--> ????????????<dependency> ????????????????????????<groupId>javax.servlet</groupId> ????????????????????????<artifactId>javax.servlet-api</artifactId> ????????????????????????<version>${servlet}</version> ????????????</dependency> ????????????<!--?JSTL?--> ????????????<dependency> ????????????????????????<groupId>jstl</groupId> ????????????????????????<artifactId>jstl</artifactId> ????????????????????????<version>${jstl}</version> ????????????</dependency> ????????????<!--xml解析支持?--> ????????????<dependency> ????????????????????????<groupId>com.thoughtworks.xstream</groupId> ????????????????????????<artifactId>xstream</artifactId> ????????????????????????<version>${xstream}</version> ????????????</dependency> </dependencies> |
Spring配置
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 | @EnableWebMvc//?啟用SpringMVC @ComponentScan(basePackages?=?"org.xdemo.example.springmvc")//?配置包掃描路徑 @Configuration//?啟用注解式配置 //繼承WebMvcConfigurerAdapter可以是我們可以重寫一些資源或者一些處理器 public?class?AppConfig?extends?WebMvcConfigurerAdapter?{ ???????????????/** ????????????????*?設(shè)置資源路徑 ????????????????*/ ???????????????@Override ???????????????public?void?addResourceHandlers(ResourceHandlerRegistry?registry)?{ ??????????????????????registry.addResourceHandler("/resources/**").addResourceLocations("/resources/").setCachePeriod(31556926); ???????????????} ???????????????/** ????????????????*?設(shè)置默認(rèn)的Servlet請求處理器 ????????????????*/ ???????????????@Override ???????????????public?void?configureDefaultServletHandling(DefaultServletHandlerConfigurer?configurer)?{ ??????????????????????configurer.enable(); ???????????????} ???????????????/** ????????????????*?設(shè)置視圖解析器,以及頁面路徑 ????????????????*? ????????????????*?@return ????????????????*/ ???????????????@Bean ???????????????public?InternalResourceViewResolver?getInternalResourceViewResolver()?{ ???????????????????????????????InternalResourceViewResolver?resolver?=?new?InternalResourceViewResolver(); ???????????????????????????????resolver.setPrefix("/WEB-INF/views/"); ???????????????????????????????resolver.setSuffix(".jsp"); ???????????????????????????????return?resolver; ???????????????} ???????????????? ???????????????/** ????????????????*?配置消息轉(zhuǎn)換器 ????????????????*/ ???????????????@Override ???????????????public?void?configureMessageConverters( ??????????????????????List<HttpMessageConverter<?>>?converters)?{converters.add(converter()); ???????????????????????????????? ???????????????} ???????????????? ???????????????/** ????????????????*?JSON格式的支持,這個(gè)很重要,只有加上這個(gè)JSON的消息轉(zhuǎn)換器,才能夠支持JSON格式數(shù)據(jù)的綁定 ????????????????*?@return ????????????????*/ ???????????????@Bean ???????????????public?MappingJacksonHttpMessageConverter?converter()?{ ??????????????????????????????MappingJacksonHttpMessageConverter?converter?=?new?MappingJacksonHttpMessageConverter(); ???????????????????????????????return?converter; ???????????????} |
總結(jié)
以上是生活随笔為你收集整理的SpringMVC学习之用户登录(二)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: android 加载图片进度条,Imag
- 下一篇: 基于注解配置简单的SpringMVC+M