@RequestMapping(value = "/changeDocMode", method = RequestMethod.GET, produces = "text/html") public String changeDocMode( HttpServletRequest request, HttpServletResponse response, Model model) { HttpSession session = request.getSession(); session.removeAttribute("xUaCompatibleTag"); String userAgent = request.getHeader("User-Agent"); UserAgent userAgentObj = BroswerUtils.parseUserAgent(userAgent); Browser browser = userAgentObj.getBrowser(); if (browser.toString().equalsIgnoreCase("ie8")) { // String metaTag = "<meta http-equiv=\"X-UA-Compatible\" content=\"IE=7\"/>"; String metaTag = "http-equiv=\"X-UA-Compatible\" content=\"IE=7\""; session.setAttribute("xUaCompatibleTag", metaTag); model.addAttribute("ua", userAgent); model.addAttribute("userAgent", userAgentObj); } else { model.addAttribute("notie8", "This will only work if you are on IE8."); } return "browserUtils/detect"; }
@RequestMapping( value = "/changeDocModeUseHeader", method = RequestMethod.GET, produces = "text/html") public String changeDocModeUseHeader( HttpServletRequest request, HttpServletResponse response, Model model) { HttpSession session = request.getSession(); session.removeAttribute("xUaCompatibleTag"); String userAgent = request.getHeader("User-Agent"); UserAgent userAgentObj = BroswerUtils.parseUserAgent(userAgent); Browser browser = userAgentObj.getBrowser(); if (browser.toString().equalsIgnoreCase("ie8")) { response.addHeader("x-ua-compatible", "IE=7"); // "IE=EmulateIE7"); model.addAttribute("ua", userAgent); model.addAttribute("userAgent", userAgentObj); } else { model.addAttribute("notie8", "This will only work if you are on IE8."); } return "browserUtils/detect"; }
/** * 获取用户代理对象 * * @param request * @return */ public static UserAgent getUserAgent(HttpServletRequest request) { return UserAgent.parseUserAgentString(request.getHeader("User-Agent")); }