springboot 重定向(redirect前缀)


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对象不能传递

发布了320 篇原创文章 · 获赞 91 · 访问量 1万+

猜你喜欢

转载自blog.csdn.net/weixin_43931625/article/details/103620515