//ModelAndView @RequestMapping(value = "testModelAndView") public ModelAndView testModelAndView(){ //既有数据又有视图 ModelAndView mv=new ModelAndView("success");//view,/views/success.jsp Student student = new Student(); student.setId(1); student.setName("zs"); mv.addObject("student",student);//相当于request.setAtttribute return mv; }
${requestScope.student.id}-${requestScope.student.name}<br/>
//ModelMap @RequestMapping(value = "testModelMap") public String testModelMap(ModelMap map){ //既有数据又有视图 Student student = new Student(); student.setId(1); student.setName("zs"); map.put("student1",student);//request域 return "success";//view }
@SessionAttributes("student3,student")//如果要在request作用域中存放student4对象,则同时将该对象放入session作用域 @SessionAttributes(types = Student.class)//如果要在request作用域中存放student类型的对象,则同时将该类型的对象放入session作用域
//查询 @ModelAttribute //在任何请求前,都先执行@ModelAttribute修饰的方法 public void queryStudentById(Map<String,Object> map){ //模拟调用三层查询数据库的操作 Student student = new Student(); student.setId(3); student.setName("zs"); student.setAge(23); //map.put("student",student);//map的key就是与方法(testModelAttribute)参数类型的首字母的小写后一致 map.put("stu",student); } //修改,ModelAttribute @RequestMapping(value = "testModelAttribute") public String testModelAttribute(@ModelAttribute("stu") Student student){ student.setName(student.getName());//将名字修改为ls System.out.println(student.getId()+","+student.getName()+","+student.getAge()); return "success"; }
原文:https://www.cnblogs.com/ghlz/p/13510702.html