1. 程式人生 > >spring在service層獲取session和request

spring在service層獲取session和request

首先要在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();