首先要在web.xml增加如下代码:
<listener> <listener-class>org.springframework.web.context.request.RequestContextListener</listener-class>
</listener>
service层调用
@Autowired private HttpSession session; @Autowired private HttpServletRequest request;
普通类中
// 这里整体是一行
HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();