欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

RedirectAttributes 之 IE8请求跳转失败

程序员文章站 2023-01-11 21:02:17
1、时间真快,一晃又快冬天了,下了第一场雪。雪花漫漫,堵车悠悠。 2、这次遇到这样一个问题,就是RedirectAttributes传递数据参数,如果参数数据过大,在IE8浏览器时候会跳转不过去。其实发现,很多时候都是IE8给我们找麻烦。因为在IE9以上还有谷歌等其他浏览器基本都是可以携带大量的长度 ......

1、时间真快,一晃又快冬天了,下了第一场雪。雪花漫漫,堵车悠悠。

2、这次遇到这样一个问题,就是redirectattributes传递数据参数,如果参数数据过大,在ie8浏览器时候会跳转不过去。其实发现,很多时候都是ie8给我们找麻烦。因为在ie9以上还有谷歌等其他浏览器基本都是可以携带大量的长度的数据参数。所以之前的工程估计也是没有注意到这一点。

3、先上一个问题原型代码

@requestmapping("/test1.do")
    public string test1(httpservletrequest request, httpservletresponse response, redirectattributes attr) throws exception {
        
        attr.addattribute("content", 非常长的一串字符串,这里我就不写了哈,靠想象);
                return "redirect:/test2.do";
}

@requestmapping("/test2.do")
    public string test2(httpservletrequest request, httpservletresponse response) throws exception {

}

4、我们想要获取content参数的值,那么我想到了几种方法。

5、方法一,我们知道redirectattributes的addattribute()方法其实是一种get方式,那么参数就会拼接到url上,这样ie8超过限制的长度,就无法跳转了。那么其实redirectattributes也给提供了另一种传参方式addflashattribute(),这个是相当月post方式,不会再地址栏上拼接参数,那么我们如何接收这种方式的参数呢。也有两个方案,接收方案一:

@requestmapping("/test1.do")
    public string test1(httpservletrequest request, httpservletresponse response, redirectattributes attr) throws exception {
        
        attr.addflashattribute("content", 非常长的一串字符串,这里我就不写了哈,靠想象);
                return "redirect:/test2.do";
}

@requestmapping("/test2.do")
    public string test2(httpservletrequest request, httpservletresponse response,@requestparam(value="content") string content) throws exception {
            string result = content;
}       

6、方案一很容易发现是利用spring的注释方法@requestparam(value="content") string content来获取的。但是有时候这种方法不方便使用,那么还有一个方案二:

@requestmapping("/test1.do")
    public string test1(httpservletrequest request, httpservletresponse response, redirectattributes attr) throws exception {
        
        attr.addflashattribute("content", 非常长的一串字符串,这里我就不写了哈,靠想象);
                return "redirect:/test2.do";
}

@requestmapping("/test2.do")
    public string test2(httpservletrequest request, httpservletresponse response) throws exception {
     //1.requestcontextutils是spring提供的类。
        map<string, string> flashmap = (map<string, string>) requestcontextutils.getinputflashmap(request);
        string a = flashmap.get("content");
} 

7、这个也是运用了spring的requestcontextutils类获取到参数content

8、如果这两个方案都不方便使用,也有一种简单便捷的方式,就是利用session,不过这种方式要考虑并发高的时候session的影响,要根据场景来判断

@requestmapping("/test1.do")
    public string test1(httpservletrequest request, httpservletresponse response, redirectattributes attr) throws exception {
        
        request.getsession().setattribute("content", 非常长的一串字符串,这里我就不写了哈,靠想象);
                return "redirect:/test2.do";
}

@requestmapping("/test2.do")
    public string test2(httpservletrequest request, httpservletresponse response) throws exception {
          //2.运用session 删除session
                string content = (string) request.getsession().getattribute("content");
                request.getsession().removeattribute("content");
}

9、好了,目前就想到了这几个方案,不知道能否帮助到各位,如果有好的方案也希望大家教我,谢谢