springboot 重定向(RedirectAttributes)
*******************************
相关类及接口
RedirectAttributes
public interface RedirectAttributes extends Model {
RedirectAttributes addAttribute(String var1, @Nullable Object var2);
RedirectAttributes addAttribute(Object var1);
RedirectAttributes addAllAttributes(Collection<?> var1);
RedirectAttributes mergeAttributes(Map<String, ?> var1);
RedirectAttributes addFlashAttribute(String var1, @Nullable Object var2);
RedirectAttributes addFlashAttribute(Object var1);
Map<String, ?> getFlashAttributes();
}
RedirectAttributesModelMap
public class RedirectAttributesModelMap extends ModelMap implements RedirectAttributes {
@Nullable
private final DataBinder dataBinder;
private final ModelMap flashAttributes;
******************
构造方法
public RedirectAttributesModelMap() {
public RedirectAttributesModelMap(@Nullable DataBinder dataBinder) {
******************
普通方法
public RedirectAttributesModelMap addAttribute(String attributeName, @Nullable Object attributeValue) {
public RedirectAttributesModelMap addAttribute(Object attributeValue) {
public RedirectAttributesModelMap addAllAttributes(@Nullable Collection<?> attributeValues) {
public RedirectAttributesModelMap addAllAttributes(@Nullable Map<String, ?> attributes) {
public RedirectAttributesModelMap mergeAttributes(@Nullable Map<String, ?> attributes) {
public RedirectAttributes addFlashAttribute(String attributeName, @Nullable Object attributeValue) {
public RedirectAttributes addFlashAttribute(Object attributeValue) {
//使用该方法将对象添加到session中,重定向后删除数据
public Map<String, ?> getFlashAttributes() {
public Map<String, Object> asMap() {
@Nullable
private String formatValue(@Nullable Object value) {
public Object put(String key, @Nullable Object value) {
public void putAll(@Nullable Map<? extends String, ? extends Object> map) {
*************************************
示例
@RestController
public class Hello3Controller {
@RequestMapping("/hello3")
public ModelAndView hello3(RedirectAttributes redirectAttributes){
ModelAndView mv=new ModelAndView();
mv.setViewName("redirect:/redirect3");
Person person=new Person();
person.setName("瓜田李下");
person.setAge(20);
redirectAttributes.addFlashAttribute("person",person);
redirectAttributes.addAttribute("name","瓜田李下");
redirectAttributes.addFlashAttribute("name2","海贼王");
return mv;
}
@RequestMapping("/redirect3")
public String redirect3(String name,@ModelAttribute("name2") String name2,Person person,@ModelAttribute("person") Person person2){
System.out.println(name+" "+name2);
System.out.println(person);
System.out.println(person2);
return "redirect3";
}
}
************************
控制台输出
瓜田李下 海贼王
Person(name=瓜田李下, age=20)
Person(name=瓜田李下, age=20)
说明:使用redirectAttributes可以有效的传递pojo对象