SpringMVC归纳-1(model数据模型与重定向传参技术)

时间:2023-03-08 23:58:24
SpringMVC归纳-1(model数据模型与重定向传参技术)

  要点:

  1. model是一个Map结构的数据模型,能重定向时传递数据(拼接URL),但不安全,主要用于渲染前端页面,配合Thymeleaf填充html里面里设置好的参数。
  2. @RequestParam用来获取查询字符串的参数值。
  3. HttpServletRequest也可以获取查询字符串的参数值。
  4. redirect: 用于重定向到新的url。
  5. @ModelAttribute:运用在参数上,会将客户端传递过来的参数按名称注入到指定对象中,并且会将这个对象自动加入ModelMap中,便于View层使用。
  6. @ModelAttribute:运用在方法上,会在每一个@RequestMapping标注的方法前执行,如果有返回值,则自动将该返回值加入到ModelMap中。
  7. redirectAttribute.addAttribute实现URL字符串拼接,类似于model.addAttribute,但是它并不把数据添加到model里。
  8. redirectAttribute.addFlashAttribute是安全的传参方法。

   下面是以上几点的实例:

  

 package com.example.demo.controller;

 import javax.servlet.http.HttpServletRequest;

 import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody; @Controller
public class ModelConclusion { /*
*从查询字符串里获取参数username的值
* 把name添加到model模型里
* 重定向到新的页面
*/
@RequestMapping("user")
public String setAttribute(@RequestParam(value="username",defaultValue="Leo") String name,Model model) {
model.addAttribute("name",name);
return "redirect:user/1";
} /*
* 再次绑定name到model * 查看req请求参数,发现添加到model里的属性也可以在请求参数中获得
*/
@RequestMapping("user/1")
@ResponseBody()
public String getAttribute(@ModelAttribute("name") String name,Model model,HttpServletRequest req) { String modelName="model->name:"+name;
String modelString = "model:"+model.toString();
String reqName = "req->name:"+req.getParameter("name"); return modelName+"<br>"+modelString+"<br>"+reqName;
} }

  页面输出结果:

  SpringMVC归纳-1(model数据模型与重定向传参技术)

  发现,model里的数据添加到了URL里,从这一特点可以知道model传递数据是不安全的。所以我们使用model主要是因为java request没有与视图技术绑定,而非作为重定向时暂存一些重要数据,如密码。

  另外,在两个方法参数里,系统都自动创建了新的model,所以重定向后的model不在被保留,但是通过@ModelAttribute再次将数据绑定在model里。

  重新写一下setAttribute方法,以体现modelattibute绑定功能:

 @RequestMapping("user")
public String setAttribute(@ModelAttribute("name") String name,Model model) {
return "redirect:user/1";
}

  @ModelAttribute用于方法前面时,先于所在Controller下的RequestMapping标注的所有方法执行,实例如下:

  User:

 package com.example.demo.service;

 public class User {

     private String name;
private Integer age;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
} }

  Controller:

 package com.example.demo.controller;

 import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody; import com.example.demo.service.User; @Controller
public class ModelConclusion3 { /*
* @ModelAttribute注解在方法前,在所有mapping前执行
* 可以实现统一配置
*/
//绑定参数到对象属性
@ModelAttribute
public User create(User newUser) {
return newUser;
} //获取名字
@RequestMapping("user/getname")
@ResponseBody()
public String getName(User newUser) {
return newUser.getName();
} //获取年龄
@RequestMapping("user/getage")
@ResponseBody()
public Integer getAge(User newUser) {
return newUser.getAge();
} }

  

  在来看另一种重定向传参技术:

  

 package com.example.demo.controller;

 import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributes; @Controller
public class ModelConclusion2 { /*
* redirectAttribute.addAttribute功能时字符串拼接,类似于model.addAttribute,但是它并不把数据添加到model里
* redirectAttribute.addFlashAttribute时安全的传递参数方法,原理是将数据添加到session里,等页面渲染好后从session里移除,最后加入到model模型里
*/ @RequestMapping("user2")
public String setAttribute(Model model,RedirectAttributes redirectAttribute) { redirectAttribute.addAttribute("name","Jack");
redirectAttribute.addFlashAttribute("age",15);
System.out.println(model); return "redirect:user2/1";
} @RequestMapping("user2/1")
@ResponseBody()
public String getAttribute(String name,Integer age,Model model) { System.out.println(age);
System.out.println(name);
System.out.println(model); return "hello";
} }

  控制台结果:

  SpringMVC归纳-1(model数据模型与重定向传参技术)

  可以发现,addflash后model里是没数据的,而且由于它不是URL拼接,所以age也没有捕获到,但最后我们还是在model里找到它,所以addflash非常适合密码等信息的传递。

  

  上述如有错误,望请指正!