spring mvc中RequestContextHolder分析
阿新 • • 發佈:2018-12-10
RequestContextHolder
當我們需要在service層獲取request和response時,我們可以在RequestContextHolder中獲取。 使用方法:
RequestAttributes requsetAttributes = RequestContextHolder.currentRequestAttributes(); //RequestAttributes requsetAttributes = RequestContextHolder.getRequestAttributes(); String str = (String) requsetAttributes.getAttribute("name", RequestAttributes.SCOPE_SESSION); HttpServletRequest request = ((ServletRequestAttributes) requsetAttributes).getRequest(); HttpServletResponse reponse = ((ServletRequestAttributes) requsetAttributes).getResponse();