1. 程式人生 > >spring mvc中RequestContextHolder分析

spring mvc中RequestContextHolder分析

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();