生活随笔
收集整理的這篇文章主要介紹了
Spring mvc 中处理模型数据 ModelAndView
小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
Spring mvc?中處理模型數(shù)據(jù) ?ModelAndView
原文:?http://blog.csdn.net/jingyuan_/article/details/9625125
將模型數(shù)據(jù)暴露給視圖是Spring MVC框架的一項(xiàng)重要工作,SpringMVC提供了多種途徑輸出模型數(shù)據(jù):
1.ModelAndView
2.@ModelAttribute
3.Map及Model
4.@SessionAttributes
例子如下:
[java]?view plaincopy
package?com.test.controller;?? ?? import?java.util.Map;?? ?? import?org.springframework.stereotype.Controller;?? import?org.springframework.ui.ModelMap;?? import?org.springframework.web.bind.annotation.ModelAttribute;?? import?org.springframework.web.bind.annotation.RequestMapping;?? import?org.springframework.web.servlet.ModelAndView;?? ?? import?com.test.model.User;?? ?? @Controller?? @RequestMapping("/mavc")?? public?class?ModelAndViewController?{?? ????? ? ? ?? ????@RequestMapping("method1")?? ????public?ModelAndView?method1(){?? ????????ModelAndView?mav?=?new?ModelAndView();?? ????????User?u?=?new?User();?? ????????u.setUserName("1");?? ????????mav.addObject("user1",?u);?? ????????mav.setViewName("modelandview");?? ????????return?mav;?? ????}?? ?????? ????? ? ? ? ?? ????@RequestMapping("method2")?? ????public?String?method2(@ModelAttribute("user")User?user){?? ????????user.setUserName("湯姆");?? ????????return?"/mavc/handle61";?? ????}?? ?????? ????@ModelAttribute("user")?? ????public?User?getUser(){?? ????????User?user?=?new?User();?? ????????user.setUserName("湯姆");?? ????????return?user;?? ????}?? ????? ? ? ? ?? ????@RequestMapping("method3")?? ????public?String?method3(Map<String,Object>?map){?? ????????User?user?=?new?User();?? ????????user.setRealName("湯姆");?? ????????map.put("user",?user);?? ????????return?"/mavc/method3";?? ????}?? ????@RequestMapping("method4")?? ????public?String?method4(ModelMap?modelMap){?? ????????User?user?=?new?User();?? ????????user.setRealName("湯姆");?? ????????modelMap.put("user",?user);?? ????????return?"/mavc/method4";?? ????}?? ?????? ????? ? ? ?? }??
[java]?view plaincopy
package?com.test.controller;?? ?? import?org.springframework.stereotype.Controller;?? import?org.springframework.ui.ModelMap;?? import?org.springframework.web.bind.annotation.ModelAttribute;?? import?org.springframework.web.bind.annotation.RequestMapping;?? import?org.springframework.web.bind.annotation.SessionAttributes;?? import?org.springframework.web.bind.support.SessionStatus;?? ?? import?com.test.model.User;?? ?? @Controller?? @RequestMapping("/sac")?? @SessionAttributes("user")?? public?class?SessionAttributesController?{?? ?????? ????@ModelAttribute("user")?? ????public?User?getUser(){?? ????????User?user?=?new?User();?? ????????user.setUserName("湯姆");?? ????????return?user;?? ????}?? ?????? ????@RequestMapping("method1")?? ????public?String?method1(@ModelAttribute("user")?User?user){?? ????????user.setUserName("Tom");?? ????????return?"redirect:method2";?? ????}?? ????@RequestMapping("method2")?? ????public?String?method2(ModelMap?modelMap,SessionStatus?sessionStatus){?? ????????User?user?=?(User)modelMap.get("User");?? ????????if(user?!=?null){?? ????????????user.setUserName("Tim");?? ????????????sessionStatus.setComplete();?? ????????}?? ????????return?"sac/method2";?? ????}?? }??
總結(jié)
以上是生活随笔為你收集整理的Spring mvc 中处理模型数据 ModelAndView的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。