RequestContextHolder 工具类中存有我们的 request请求对象 response响应对象 session对象
我们程序员可以在项目的任何方法中获取,方便我们使用,从而不需要在形参上面注入才能获取到对象
package com.gzcgxt.http; import javax.servlet.http.HttpServletRequest; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.context.request.RequestContextHolder; import org.springframework.web.context.request.ServletRequestAttributes; /** * SpringMVC提供工具类 RequestContextHolder使用 * <p>Title: RequestContextHolderController</p> * <p>Description: </p> * <p>Company: www.baidu.com</p> * @author 刘诗华 微信:15390725037 * @date 2019-7-16下午9:42:55 * @version 1.0 */ //RequestContextHolder-->ServletRequestAttributes-->HttpServletRequest @Controller public class RequestContextHolderController { /** * 获取请求参数 * <p>Title: req</p> * <p>Description: </p> * @return */ @RequestMapping("/req") public String req() { //获取 RequestAttributes 子类对象强转(ServletRequestAttributes) ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder .getRequestAttributes(); //获取请求参数 HttpServletRequest request = attributes.getRequest(); return ""; } }
实际项目中的 User工具类