关于springmvc重定向后Cannot create a session after the response has been committed问题
首先先上代码吧,我在用springmvc进行response.sendRedirect(url);操作后报了Cannot create a session after the response has been committed错误。
@RequestMapping(value={"","/"}) public String index(HttpServletResponse response,HttpServletRequest request) throws Exception{ // String callbackurl = "http://127.0.0.1:8080/sucstepms/index/goqrcodescansign"; String callbackurl = request.getRequestURL().toString(); Authorize au = new Authorize(appKey, appSecret); String code = request.getParameter("code"); String url = au.forwardurl(callbackurl, "test", Authorize.DISPLAY_TAG_T.WEB); if ( code == null || code.isEmpty() ){ response.sendRedirect(url); return null; }else{ String text=au.querytoken(code, callbackurl); Map<String, Object> map=JSON.parseObject(text); String token=(String) map.get("access_token"); if(token==null){ response.sendRedirect(url); return null; }else{ User user=new User(token); System.out.println(user.me()); System.out.println(text+"--------------"); } } return "sign/front/guide"; }
在之前是没有加return null的,在执行重定向后悔跳到最后执行springmvc的return 页面操作。所以会报这个错误。所以解决办法就是加上return null;
我觉得这个问题的主要原因就在重定向response提交后,后面springmvc继续重定向了页面,或者多session有某些操作,所以会报这种错误。