前言:本文主要介绍SpringMVC处理模型数据的方式,包括以Map、Model、ModelAndView作为模型数据,介绍指定响应页面的方式。
前台测试界面(type、username、password用来接收后台传来的数据,以检验后台模型数据的正确性 ):
1. 测试页面传参(ModelAndView——构造函数设置view)
Spring Web MVC 提供Model、Map或ModelMap让我们能去暴露渲染视图需要的模型数据,ModelAndView中既有模型数据信息,也有View页面信息。此处在功能方法中,以ModelAndView构造函数方式传入待返回的View页面,通过API添加数据模型,SpringMVC会把此数据模型中的数据反馈给View。
前台代码:
<form action="getModelAndView.action" method="post">
测试页面传参(ModelAndView——构造函数设置view):<button type="submit">提交</button>
</form>
后台代码:
@RequestMapping("/getModelAndView.action")
public ModelAndView getModelAndView(){
System.out.println("【ModelAndView】【构造函数】");
/* ModelAndView构造函数可以指定返回页面的名称. */
/* 此处返回的页面为:'/views/handleModel.jsp'. */
ModelAndView modelAndView = new ModelAndView("handleModel");
modelAndView.addObject("type", "ModelAndView_构造函数设置View");
return modelAndView;
}
2. 测试页面传参(ModelAndView——setViewName)
此处在功能方法中,通过API设置待返回的View页面,通过API添加数据模型,SpringMVC会把此数据模型中的数据反馈给View。
前台代码:
<form action="getModelAndView2.action" method="post">
测试页面传参(ModelAndView——setViewName):<button type="submit">提交</button>
</form>
后台代码:
@RequestMapping("/getModelAndView2.action")
public ModelAndView getModelAndView2(){
System.out.println("【ModelAndView】【setViewName】");
ModelAndView modelAndView = new ModelAndView();
modelAndView.addObject("type", "ModelAndView_setViewName");
/* setViewName方法来设置所需要跳转的页面. */
modelAndView.setViewName("handleModel");
return modelAndView;
}
3. 测试页面传参(Map为形参)
此功能方法中,以Map为作为功能方法的形参,SpringMVC自动将此Map中的数据暴露为模型数据。
前台代码:
<form action="getMap.action" method="post">
测试页面传参(Map为形参):<button type="submit">提交</button>
</form>
后台代码:
@RequestMapping("/getMap.action")
public String getMap2(Map<String, Object> map){
System.out.println("【Map为形参】【返回值为页面】");
/* map.put相当于request.setAttribute方法. */
map.put("type", "map以形参返回Model");
/* 返回的页面:/views/handleModel.jsp */
return "handleModel";
}
4. 测试页面传参(Model为形参)
此功能方法中,以Model为作为功能方法的形参,SpringMVC自动将此Model中的数据暴露为模型数据。
前台代码:
<form action="getModel.action" method="post">
测试页面传参(Model为形参):<button type="submit">提交</button>
</form>
后台代码:
@RequestMapping("/getModel.action")
public String getModel(Model model){
System.out.println("【Model为形参】【返回值为页面】");
/* model.addAttribute相当于request.setAttribute方法. */
model.addAttribute("type", "model以形参返回Model");
/* 返回的页面:/views/handleModel.jsp */
return "handleModel";
}
5. 测试页面传参(ModelMap为形参)
此功能方法中,以ModelMap为作为功能方法的形参,SpringMVC自动将此ModelMap中的数据暴露为模型数据。
前台代码:
<form action="getModelMap.action" method="post">
测试页面传参(ModelMap为形参):<button type="submit">提交</button>
</form>
后台代码:
@RequestMapping("/getModelMap.action")
public String getModelMap(ModelMap mapModel){
System.out.println("【ModelMap为形参】【返回值为页面】");
/* mapModel.addAttribute相当于request.setAttribute方法. */
mapModel.addAttribute("type", "ModelMap以形参返回Model");
/* 返回的页面:/views/handleModel.jsp */
return "handleModel";
}
6. 测试页面传参(@ModelAttribute修饰形参)
@ModelAttribute有三大作用:
- 绑定请求参数到命令对象:放在功能处理方法的入参上时,用于将多个请求参数绑定到一个命令对象,从而简化绑定流程,而且自动暴露为模型数据用于视图页面展示时使用;
- 暴露表单引用对象为模型数据:放在处理器的一般方法(非功能处理方法)上时,是为表单准备要展示的表单引用对象;
- 暴露@RequestMapping方法返回值为模型数据:放在功能处理方法的返回值上时,是暴露功能处理方法的返回值为模型数据,用于视图页面展示时使用。
此处仅给出作用1、作用3,作用2在参数绑定一章已给出样例,此处不再重复。
先是作用1,@ModelAttribute修饰形参,绑定请求参数到命令对象,自动暴露为模式数据。此样例中,前台并未传参数,@ModelAttribute仅是将其修饰的参数暴露给模型数据。
前台代码:
<form action="getModelAttribute.action" method="post">
测试页面传参(@ModelAttribute修饰形参):<button type="submit">提交</button>
</form>
后台代码:
RequestMapping("/getModelAttribute.action")
public String getModelAttribute(@ModelAttribute("user") UserVo user){
System.out.println("【@ModelAttribute修饰形参】【返回值为页面】");
user.setUsername("username_return");
user.setPassword("password_return");
/* 返回的页面:/views/handleModel.jsp */
return "handleModel";
}
7. 测试页面传参(@ModelAttribute修饰返回值)
此样例给出@ModelAttribute第三个作用:暴露@RequestMapping 方法返回值为模型数据。当@ModelAttribute放在功能处理方法的返回值上时,是暴露功能处理方法的返回值为模型数据,用于视图页面展示时使用。
前台代码:
<form action="getModelAttribute2.action" method="post">
测试页面传参(@ModelAttribute修饰返回值):<button type="submit">提交</button>
(会返回出错页面,因为后台默认根据请求路径(/views/views/getModelAttribute2.jsp)寻找响应页面,必然出错)
</form>
后台代码:
@RequestMapping("/getModelAttribute2.action")
public @ModelAttribute("user") UserVo getModelAttribute2(){
System.out.println("测试页面传值方式:getModelAttribute2");
UserVo user = new UserVo();
user.setUsername("username_return");
user.setPassword("password_return");
/* 响应的view应该也是该请求的view,等同于void返回. */
/* 返回的页面:/views/views/getModelAttribute2.jsp */
return user;
}