当前位置: 移动技术网 > IT编程>开发语言>Java > SpringMVC 跨重定向请求传递数据的方法实现

SpringMVC 跨重定向请求传递数据的方法实现

2020年06月13日  | 移动技术网IT编程  | 我要评论

正气寻妇录,商道 王凯,一路向西完整版快播

执行完post请求后,通常来讲一个最佳实践就是执行重定向。重定向将丢弃原始请求数据,原始请求中的模型数据和请求都会消亡。可以有效避免用户浏览器刷新或者后退等操作,直接间接地重复执行已经完成的post请求。

在控制方法中返回的视图名称中,在string前使用"redirect:"前缀,那么这个string就不是来查找视图的,而是浏览器进行重定向的路径,相当于重新发出请求。

重定向通常相当于从一个controller到另一个controller。 

(1)使用url模板以路径变量和查询参数的形式传递数据(一些简单的数据)

@getmapping("/home/index")
  public string index(model model){
    meinv meinv = new meinv("gaoxing",22);
    model.addattribute("lastname",meinv.getlastname());
    model.addattribute("age",meinv.getage());
    return "redirect:/home/details/{lastname}";
  }

  @getmapping("/home/details/{lastname}")
  public string details(@pathvariable string lastname, @requestparam integer age){
    system.out.println(lastname);
    system.out.println(age);
    return "home";
  }

(2)通过flash属性发送数据(对象等复杂数据)

@getmapping("/home/index")
  public string index(redirectattributes model){
    meinv meinv = new meinv("gaoxing",22);
    model.addattribute("lastname",meinv.getlastname());
    model.addflashattribute("meinv",meinv);
    return "redirect:/home/details/{lastname}";
  }

  @getmapping("/home/details/{lastname}")
  public string details(@pathvariable string lastname, model model){
    meinv meinv = null;
    if(model.containsattribute("meinv")){
      meinv = (meinv) model.asmap().get("meinv");
    }
    system.out.println(meinv);
    return "home";
  }

到此这篇关于springmvc 跨重定向请求传递数据的方法实现的文章就介绍到这了,更多相关springmvc 跨重定向传递数据内容请搜索移动技术网以前的文章或继续浏览下面的相关文章希望大家以后多多支持移动技术网!

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网