一、 使用ModelAndVIew处理模型数据
控制器处理方法的返回值如果为ModelAndView, 则其既包含视图信息,也包含模型数据信息。数据是放在请求域中的。
//使用ModelAndView @RequestMapping("/output3") public ModelAndView output3(){ ModelAndView modelAndView = new ModelAndView("success"); //viewName即为跳转页面 modelAndView.addObject("msg","ModelAndView处理数据"); return modelAndView; }
二、使用Map处理模型数据
可以在方法的参数列表传入Map或者Model或者ModelMap,这些参数里面保存的所有数据都会放在request请求域中,可以在页面中获取这些数据。
@Controller public class OutputController { //使用Map @RequestMapping("/output") public String output(Map<String, Object> map){ map.put("msg","输出数据处理"); return "success"; } //使用Model,一个接口 @RequestMapping("/output1") public String output1(Model model){ model.addAttribute("msg","model处理数据"); return "success"; } //使用ModelMap @RequestMapping("/output2") public String output2(ModelMap modelMap){ modelMap.addAttribute("msg","modelMap处理数据"); return "success"; } }
实际上本文来源gaodai$ma#com搞$$代**码网Map、Model、ModelMap最终实现都是BindingAwareModelMap,相当于BindingAwareModelMap中保存的数据都会被放在请求域中。
Map是JDK中的一个interface,Model是Spring中的一个interface,而ModelMap是Spring中的一个Class
ModelMap源码中实际上是继承LinkedHashMap类,所以本质上属于Map接口的一个实现类
public class ModelMap extends LinkedHashMap<String, Object>
BindingAwareModelMap源码中继承ExtendedModelMap类,而ExtendedModelMap这个类又继承于ModelMap类,同时实现Model接口。
public class BindingAwareModelMap extends ExtendedModelMap public class ExtendedModelMap extends ModelMap implements Model
所以Map、Model、ModelMap三者关系如下:
三、使用@SessionAttributes注解处理模型数据
SpringMVC提供了一种可以临时给Session域中保存数据的方式,即使用@SessionAttributes注解,这个注解只能作用在类上。
//给BindingAwareModelMap中保存的数据,同时在session中也保存一份,value指定保存数据时要给session中放的数据的key //type只要是指定的类型的数据,session就会保存 @SessionAttributes(value = "msg",types = {String.class}) @Controller public class OutputController
四、使用@ModelAttribute注解处理模型数据
某些业务场景不需要全字段更新,比如修改book对象信息,bookName只读而不能修改,只有其中某写字段的值可以修改。如果让SpringMVC去new一个对象,某些字段会有默认值,将new出来的对象去更新数据库的值,很有可能会发生null值覆盖了原来不能修改的字段的值。
所以,SpringMVC要封装请求参数的Book对象不应该是自己new出来的,而应该是从数据库中取出来的对象,使用这个对象来封装请求参数,这样只是修改了指定的字段值,没有修改的字段值保持原来的值。