http 412问题

最近项目发现了一个问题,每次刷新界面,都会提示:

然后点击确定,界面都会出现412

再次点击刷新,界面又恢复了,之后再刷新,问题又出现了,再刷新,界面又恢复了,然后得出:

问题只在chrome出现,其他浏览器IE什么的没有发现,打开chrome的开发者模式,这个问题也不会出现;

之后,开始看代码,

注释掉所有的js,发现问题依旧出现。。。

最后定位在java代码,

之前大概是/login post请求直接返回string,通过springmvc的viewResolver直接解析,返回到html,导致每次刷新主页,js会重新请求/login post导致412

    @RequestMapping(value = "/login", method = RequestMethod.POST)
    public String doLoginHandler(RedirectAttributes attributes, HttpSession session, HttpServletRequest req, HttpServletResponse resp)
    {

        String toAddress = returnLoginmsg(session, req, resp, "0", userName, password, "0");
        return toAddress;
    }

修改为

    @RequestMapping(value = "/login", method = RequestMethod.POST)
    public String doLoginHandler(RedirectAttributes attributes, HttpSession session, HttpServletRequest req, HttpServletResponse resp)
    {

        String toAddress = returnLoginmsg(session, req, resp, "0", userName, password, "0");
        attributes.addAttribute("toAddress", toAddress);
        return "redirect:index";
    }

    @RequestMapping(value={"/index"},  method={RequestMethod.GET})
    public String doIndex(HttpServletRequest req)  {
        return req.getParameter("toAddress");
    }

登陆成功后跳转到/index get请求,再次刷新会直接调取get请求,而不会再次请求/login post。

Ride the wave as long as it will take you.
原文地址:https://www.cnblogs.com/jianpanaq/p/10955229.html