要点:
- model是一个Map结构的数据模型,能重定向时传递数据(拼接URL),但不安全,主要用于渲染前端页面,配合Thymeleaf填充html里面里设置好的参数。
- @RequestParam用来获取查询字符串的参数值。
- HttpServletRequest也可以获取查询字符串的参数值。
- redirect: 用于重定向到新的url。
- @ModelAttribute:运用在参数上,会将客户端传递过来的参数按名称注入到指定对象中,并且会将这个对象自动加入ModelMap中,便于View层使用。
- @ModelAttribute:运用在方法上,会在每一个@RequestMapping标注的方法前执行,如果有返回值,则自动将该返回值加入到ModelMap中。
- redirectAttribute.addAttribute实现URL字符串拼接,类似于model.addAttribute,但是它并不把数据添加到model里。
- redirectAttribute.addFlashAttribute是安全的传参方法。
下面是以上几点的实例:
1 package com.example.demo.controller; 2 3 4 5 import javax.servlet.http.HttpServletRequest; 6 7 import org.springframework.stereotype.Controller; 8 import org.springframework.ui.Model; 9 import org.springframework.web.bind.annotation.ModelAttribute; 10 import org.springframework.web.bind.annotation.RequestMapping; 11 import org.springframework.web.bind.annotation.RequestParam; 12 import org.springframework.web.bind.annotation.ResponseBody; 13 14 15 @Controller 16 public class ModelConclusion { 17 18 /* 19 *从查询字符串里获取参数username的值 20 * 把name添加到model模型里 21 * 重定向到新的页面 22 */ 23 @RequestMapping("user") 24 public String setAttribute(@RequestParam(value="username",defaultValue="Leo") String name,Model model) { 25 model.addAttribute("name",name); 26 return "redirect:user/1"; 27 } 28 29 /* 30 * 再次绑定name到model32 * 查看req请求参数,发现添加到model里的属性也可以在请求参数中获得 33 */ 34 @RequestMapping("user/1") 35 @ResponseBody() 36 public String getAttribute(@ModelAttribute("name") String name,Model model,HttpServletRequest req) { 37 38 String modelName="model->name:"+name; 39 String modelString = "model:"+model.toString(); 40 String reqName = "req->name:"+req.getParameter("name"); 41 42 return modelName+"
"+modelString+"
"+reqName; 43 } 44 45 }
页面输出结果:
发现,model里的数据添加到了URL里,从这一特点可以知道model传递数据是不安全的。所以我们使用model主要是因为java request没有与视图技术绑定,而非作为重定向时暂存一些重要数据,如密码。
另外,在两个方法参数里,系统都自动创建了新的model,所以重定向后的model不在被保留,但是通过@ModelAttribute再次将数据绑定在model里。
重新写一下setAttribute方法,以体现modelattibute绑定功能:
1 @RequestMapping("user") 2 public String setAttribute(@ModelAttribute("name") String name,Model model) { 3 return "redirect:user/1"; 4 }
@ModelAttribute用于方法前面时,先于所在Controller下的RequestMapping标注的所有方法执行,实例如下:
User:
1 package com.example.demo.service; 2 3 public class User { 4 5 private String name; 6 private Integer age; 7 public String getName() { 8 return name; 9 } 10 public void setName(String name) { 11 this.name = name; 12 } 13 public Integer getAge() { 14 return age; 15 } 16 public void setAge(Integer age) { 17 this.age = age; 18 } 19 20 }
Controller:
1 package com.example.demo.controller; 2 3 import org.springframework.stereotype.Controller; 4 import org.springframework.web.bind.annotation.ModelAttribute; 5 import org.springframework.web.bind.annotation.RequestMapping; 6 import org.springframework.web.bind.annotation.ResponseBody; 7 8 import com.example.demo.service.User; 9 10 @Controller 11 public class ModelConclusion3 { 12 13 /* 14 * @ModelAttribute注解在方法前,在所有mapping前执行 15 * 可以实现统一配置 16 */ 17 //绑定参数到对象属性 18 @ModelAttribute 19 public User create(User newUser) { 20 return newUser; 21 } 22 23 //获取名字 24 @RequestMapping("user/getname") 25 @ResponseBody() 26 public String getName(User newUser) { 27 return newUser.getName(); 28 } 29 30 //获取年龄 31 @RequestMapping("user/getage") 32 @ResponseBody() 33 public Integer getAge(User newUser) { 34 return newUser.getAge(); 35 } 36 37 38 }
在来看另一种重定向传参技术:
1 package com.example.demo.controller; 2 3 import org.springframework.stereotype.Controller; 4 import org.springframework.ui.Model; 5 import org.springframework.web.bind.annotation.RequestMapping; 6 import org.springframework.web.bind.annotation.ResponseBody; 7 import org.springframework.web.servlet.mvc.support.RedirectAttributes; 8 9 @Controller 10 public class ModelConclusion2 { 11 12 /* 13 * redirectAttribute.addAttribute功能时字符串拼接,类似于model.addAttribute,但是它并不把数据添加到model里 14 * redirectAttribute.addFlashAttribute时安全的传递参数方法,原理是将数据添加到session里,等页面渲染好后从session里移除,最后加入到model模型里 15 */ 16 17 @RequestMapping("user2") 18 public String setAttribute(Model model,RedirectAttributes redirectAttribute) { 19 20 redirectAttribute.addAttribute("name","Jack"); 21 redirectAttribute.addFlashAttribute("age",15); 22 System.out.println(model); 23 24 return "redirect:user2/1"; 25 } 26 27 @RequestMapping("user2/1") 28 @ResponseBody() 29 public String getAttribute(String name,Integer age,Model model) { 30 31 System.out.println(age); 32 System.out.println(name); 33 System.out.println(model); 34 35 return "hello"; 36 } 37 38 39 }
控制台结果:
可以发现,addflash后model里是没数据的,而且由于它不是URL拼接,所以age也没有捕获到,但最后我们还是在model里找到它,所以addflash非常适合密码等信息的传递。
上述如有错误,望请指正!