springboot 重定向(redirect前缀)
********************************
相关注解
@ModelAttribute:读取modelAndView中的数据
@Target({ElementType.PARAMETER, ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface ModelAttribute {
@AliasFor("name")
String value() default "";
@AliasFor("value")
String name() default "";
boolean binding() default true;
}
********************************
示例
@RestController
public class Hello3Controller {
@RequestMapping("/hello2")
public ModelAndView hello2(){
ModelAndView mv=new ModelAndView();
mv.setViewName("redirect:/redirect2");
mv.addObject("attributeName","海贼王");
Person person=new Person();
person.setName("瓜田李下");
person.setAge(20);
mv.addObject("person",person);
return mv;
}
@RequestMapping("/redirect2")
public String redirect2(String attributeName,@ModelAttribute("attributeName") String name,Person person,ModelAndView mv){
System.out.println(attributeName+" "+name);
System.out.println(person);
System.out.println(mv.getModelMap().getAttribute("attributeName"));
return "redirect2";
}
}
****************************
控制台输出
扫描二维码关注公众号,回复:
8794449 查看本文章
海贼王 海贼王
Person(name=null, age=null)
null
说明:直接在方法体内获取数据,参数可以自动映射,也可使用@ModelAttribute获取数据;这种方式只能传递字符串,pojo对象不能传递