处理模型数据
如果页面跳转时需要携带数据,则可以使用以下几种方式:
这几种方式都将数据放在了request作用域,如果要放在session作用域中,则需要加@SessionAttributes注解
ModelAndView、ModelMap、Map、Model
ModelAndView方式
- 请求页面
<a href="handler/testModelAndView">ModelAndView</a>
- 处理请求
//ModelAndView
@RequestMapping(value = "testModelAndView")
public ModelAndView testModelAndView() {
//视图就是sucess页面
ModelAndView mv=new ModelAndView("sucess");
Student student=new Student();
student.setId(1);
student.setName("lyh");
mv.addObject("student", student);//相当于request.serAttribute("student", student)
return mv;
}
3.返回页面
${requestScope.student.id}---${requestScope.student.name}
ModelMap、Map、Model方式
- 请求页面
<a href="handler/testModelMap">testModelMap</a> <br>
<a href="handler/testModel">testModel</a> <br>
<a href="handler/testMap">testMap</a> <br>
- 处理请求
//ModelAndView
@RequestMapping(value = "testModelMap")
public String testModelMap(ModelMap mm) {
Student student=new Student();
student.setId(1);
student.setName("lyh");
mm.put("student1", student);//相当于request.serAttribute("student", student)
return "sucess"; //返回页面
}
//ModelAndView
@RequestMapping(value = "testModel")
public String testModel(Model model) {
Student student=new Student();
student.setId(1);
student.setName("lyh");
model.addAttribute("student2", student);//相当于request.serAttribute("student", student)
return "sucess"; //返回页面
}
//ModelAndView
@RequestMapping(value = "testMap")
public String testMap(Map<String,Object> map) {
Student student=new Student();
student.setId(1);
student.setName("lyh");
map.put("student3", student);//相当于request.serAttribute("student", student)
return "sucess"; //返回页面
}
- 返回页面
testModelMap:${requestScope.student1.id}---${requestScope.student1.name}<br>
testModel:${requestScope.student2.id}---${requestScope.student2.name}<br>
testMap:${requestScope.student3.id}---${requestScope.student3.name}<br>
@SessionAttributes注解
页面跳转时ModelAndView、ModelMap、Map、Model这几种方式都将参数放在了request作用域,如果要放在session作用域则需要加session注解。
@SessionAttribute注解的几个属性
value:用于在session作用域中存放的对象
type:用于在session作用域中存放的类型,该类型的所有对象都会被存放在session域
示例:
@SessionAttributes("student1")//将student1放入session域
@Controller
@RequestMapping("handler")
public class SpringMvcHandler {
@RequestMapping(value = "testModelAndView")
public ModelAndView testModelAndView() {
//视图就是sucess页面
ModelAndView mv=new ModelAndView("sucess");
Student student=new Student();
student.setId(1);
student.setName("lyh");
mv.addObject("student", student);//相当于request.serAttribute("student", student)
return mv;
}
@RequestMapping(value = "testModelMap")
public String testModelMap(ModelMap mm) {
Student student=new Student();
student.setId(1);
student.setName("lyh");
mm.put("student1", student);//相当于request.serAttribute("student", student)
return "sucess"; //返回页面
}
@RequestMapping(value = "testModel")
public String testModel(Model model) {
Student student=new Student();
student.setId(1);
student.setName("lyh");
model.addAttribute("student2", student);//相当于request.serAttribute("student", student)
return "sucess"; //返回页面
}
@RequestMapping(value = "testMap")
public String testMap(Map<String,Object> map) {
Student student=new Student();
student.setId(1);
student.setName("lyh");
map.put("student3", student);//相当于request.serAttribute("student", student)
return "sucess"; //返回页面
}
}
@ ModelAttribute
1.@ModelAttribute注解经常在更新时使用
2.在不改变原有代码的基础上插入一个新方法
在请求该类的每个方法前都会执行一次@ModelAttribute修饰的方法,设计原则是基于一个控制器对应一个功能,使用时需要注意。
并且@ModelAttribute修饰的方法的参数map.put()可以将对象放入即将查询的参数中
map.put(k,v)其中的k必须是即将查询方法参数的首字母小写,如果不一致,需要通过 @ModelAttribute注解声明即将查询方法的参数,即将k写入注解中
示例:
//查询
@ModelAttribute//在任何一次请求前都会执行一次带@ModelAttribute的方法
public void queryStudentById(Map<String,Object> map) {
Student student =new Student();
student.setId(2);
student.setName("zs");
student.setAge(20);
map.put("stu", student);//约定:map的key就是方法参数类型首字母小写,不一致的话需要在参数类型前加@ModelAttribute("key")
}
//更新
@RequestMapping(value = "testModelAttribute")
public String testModelAttribute(@ModelAttribute("stu") Student student) {
student.setName(student.getName());
System.out.println(student.getId()+student.getName()+student.getAge());
return "sucess"; //返回页面
}
前台页面:
<form action="handler/testModelAttribute" method="post">
name:<input type="text" name="name"/>
<input type="submit" value="修改"/>
</form>