spring mvc在普通类中获取HttpServletRequest对象
如题,需要在web.xml中配置request监听,如下
<listener> <description>spring request监听器</description> <listener-class>org.springframework.web.context.request.RequestContextListener</listener-class> </listener>
在普通的类中使用如下
HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes())
.getRequest();