RedirectAttributes 之 IE8请求跳转失败
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、好了,目前就想到了这几个方案,不知道能否帮助到各位,如果有好的方案也希望大家教我,谢谢

浙公网安备 33010602011771号