SpringMVC处理模型数据

处理模型数据
Spring MVC 提供了以下几种途径输出模型数据:
   ModelAndView : 处理方法返回值类型为 ModelAndView 时, 方法体即可通过该对象添加模型数据
   Map 及 Model : 入参为org.springframework.ui.Model、org.springframework.ui.ModelMap 或 java.uti.Map 时,处理方法返回时,Map 中的数据会自动添加到模型中。
   @SessionAttributes : 将模型中的某个属性暂存到 HttpSession 中,以便多个请求之间可以共享这个属性
   @ModelAttribute : 方法入参标注该注解后, 入参的对象就会放到数据模型中
ModelAndView
控制器处理方法的返回值如果为 ModelAndView, 则其既包含视图信息,也包含模型数据信息。
SpringMVC 会把 ModelAndView 的 model 中数据放入到 request 域对象中.
添加模型数据:
  MoelAndView addObject(String attributeName, Object attributeValue)
  ModelAndView addAllObject(Map<String, ?> modelMap)
设置视图:
  void setView(View view )
  void setViewName(String viewName)
1.--ModelAndView:传递下一个页面需要的数据,设置转发页面

@Controller
@RequestMapping("/model")
/**
* ModelAndView:传递下一个页面需要的数据,设置转发页面
* @author Administrator
*
*/
public class ModelController {
/**
* 获取公告
*/
@RequestMapping("/m1")
public ModelAndView getNotice(int id,ModelAndView modelAndView){
//获取公告
//
Notice notice=new Notice();
notice.setId(10);
notice.setTitle("本周天不上班");
notice.setContent("由于十九大召开,本周天不上班");
//使用modelAndView对象,视图名称,模型数据
modelAndView.setViewName("hello4");
modelAndView.addObject("notice", notice);
modelAndView.addObject("a", "abc");
return modelAndView;
}
2. Map 及 Model
Spring MVC 在内部使用了一个org.springframework.ui.Model 接口存储模型数据
具体步骤
Spring MVC 在调用方法前会创建一个隐含的模型对象作为模型数据的存储容器。
如果方法的入参为 Map 或 Model 类型,Spring MVC 会将隐含模型的引用传递给这些入参。在方法体内,开发者可以通过这个入参对象访问到模型中的所有数据,也可以向模型中添加新的属性数据
@RequestMapping("/m2")
public ModelAndView getNotice2(int id,ModelAndView modelAndView){
//获取公告
//
Notice notice=new Notice();
notice.setId(id);
notice.setTitle("本周天不上班");
notice.setContent("由于十九大召开,本周天不上班");
//使用modelAndView对象,视图名称,模型数据
modelAndView.setViewName("hello4");
// modelAndView.addObject("notice", notice);
// modelAndView.addObject("a", "abc");
Map<String, Object> map=new HashMap<String, Object>();
map.put("notice", notice);
map.put("a", "bbb");
modelAndView.addAllObjects(map);
return modelAndView;
}
@RequestMapping("/m3")
public String getNotice3(Model model){
Notice notice=new Notice();
notice.setId(15);
notice.setTitle("本周天不上班");
notice.setContent("由于十九大召开,本周天不上班");
model.addAttribute("notice", notice);
model.addAttribute("a", "666");
return "hello4";
}
@RequestMapping("/m4")
public String getNotice4(Map<String , Object> map){
Notice notice=new Notice();
notice.setId(15);
notice.setTitle("本周天不上班");
notice.setContent("由于十九大召开,本周天不上班");
map.put("notice", notice);
map.put("a", "8888");
return "hello4";
}
}

3.@SessionAttributes
若希望在多个请求之间共用某个模型属性数据,则可以在控制器类上标注一个 @SessionAttributes, Spring MVC 将在模型中对应的属性暂存到 HttpSession 中。
@SessionAttributes 除了可以通过属性名指定需要放到会话中的属性外,还可以通过模型属性的对象类型指定哪些模型属性需要放到会话中
  
@Controller
@RequestMapping("/session")
//@SessionAttributes({"user","notice"})
@SessionAttributes(types={User.class,Notice.class})
/**
* @SessionAttributes("user")
* @SessionAttributes:
* 可以把模型数据当中对应的对象存储到session中
* session.setAttr("user",request.getAttr("user"))
* @author Administrator
*
*/
public class SessionController {
@RequestMapping("/se1")
public String sess1(User user,Notice notice){
return "hello6";
}
/**
* 如果参数列表中有两个类型相同的参数,只会在模型中存储一个
* 使用@ModelAttribute可以给两个类型相同的参数做区分,
* 让他们都存储在模型数据中
* 如果遇到有两个类型相同并且都需要存储到session中
* 可以使用model去解决问题
* @param user
* @param user2
* @return
*/
@RequestMapping("/se2")
public String sess2(@ModelAttribute("user")User user,@ModelAttribute("user2")User user2){
return "hello6";
}
@RequestMapping("/se3")
public String sess3(User user,User user2,Model model){
model.addAttribute("user", user);
model.addAttribute("user2", user2);
return "hello6";
}
}
4. @ModelAttribute
* @ModelAttribute的本质作用就是在模型当中添加数据
* (request.setAttribute(被@ModelAttribute所标记的对象))
* 当我们调用被@RequestMapping所标的方法时,
* 会先调用被@ModelAttribute所标记的方法

猜你喜欢

转载自blog.csdn.net/lpdfight/article/details/78290171