工程代码
github: https://github.com/dengjili/springmvc
Model
@RequestMapping("/modelData")
public ModelAndView modelData(Model model) {
ModelAndView mv = new ModelAndView();
BeanParam beanParam = new BeanParam();
beanParam.setName("测试2");
beanParam.setNote("test2");
model.addAttribute("beanParam", beanParam);
mv.setViewName("forward:./showModel");
return mv;
}
@RequestMapping("/showModel")
public ModelAndView showModel(@RequestAttribute("beanParam") BeanParam beanParam) {
logger.debug("==> {}", beanParam);
ModelAndView mv = new ModelAndView();
mv.addObject(beanParam);
mv.setView(new MappingJackson2JsonView());
return mv;
}
ModelAndView
@RequestMapping("/modelAndView")
public ModelAndView modelAndView(ModelAndView mv) {
BeanParam beanParam = new BeanParam();
beanParam.setName("测试3");
beanParam.setNote("test3");
mv.addObject("beanParam", beanParam);
mv.setViewName("forward:./showModel");
return mv;
}
@RequestMapping("/showModel")
public ModelAndView showModel(@RequestAttribute("beanParam") BeanParam beanParam) {
logger.debug("==> {}", beanParam);
ModelAndView mv = new ModelAndView();
mv.addObject(beanParam);
mv.setView(new MappingJackson2JsonView());
return mv;
}
modelAndView2
@RequestMapping("/modelAndView2")
public ModelAndView modelAndView2() {
ModelAndView mv = new ModelAndView();
BeanParam beanParam = new BeanParam();
beanParam.setName("测试3");
beanParam.setNote("test3");
mv.addObject("beanParam", beanParam);
mv.setViewName("forward:./showModel");
return mv;
}
@RequestMapping("/showModel")
public ModelAndView showModel(@RequestAttribute("beanParam") BeanParam beanParam) {
logger.debug("==> {}", beanParam);
ModelAndView mv = new ModelAndView();
mv.addObject(beanParam);
mv.setView(new MappingJackson2JsonView());
return mv;
}
modelMap
@RequestMapping("/modelMap")
public ModelAndView modelMap(ModelMap modelMap) {
ModelAndView mv = new ModelAndView();
BeanParam beanParam = new BeanParam();
beanParam.setName("测试1");
beanParam.setNote("test1");
modelMap.addAttribute("beanParam", beanParam);
mv.setViewName("forward:./showModel");
return mv;
}
@RequestMapping("/showModel")
public ModelAndView showModel(@RequestAttribute("beanParam") BeanParam beanParam) {
logger.debug("==> {}", beanParam);
ModelAndView mv = new ModelAndView();
mv.addObject(beanParam);
mv.setView(new MappingJackson2JsonView());
return mv;
}