2017-04-21 72 views
1

從servlet上下文豆這是我的web.xml我怎樣才能在過濾器

<!-- The definition of the Root Spring Container shared by all Servlets and Filters --> 
<context-param> 
    <param-name>contextConfigLocation</param-name> 
    <param-value>classpath:/spring/root-context.xml</param-value> 
</context-param> 

<!-- Creates the Spring Container shared by all Servlets and Filters --> 
<listener> 
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> 
</listener> 

<!-- Processes application requests --> 
<servlet> 
    <servlet-name>dispatcherServlet</servlet-name> 
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> 
    <init-param> 
     <param-name>contextConfigLocation</param-name> 
     <param-value>classpath:/spring/servlet-context.xml</param-value> 
    </init-param> 
    <load-on-startup>1</load-on-startup> 
</servlet> 
<servlet-mapping> 
    <servlet-name>dispatcherServlet</servlet-name> 
    <url-pattern>/</url-pattern> 
</servlet-mapping> 

而且我喜歡這個自定義過濾器。


protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) 
       throws ServletException, IOException { 
    WebApplicationContext wac = WebApplicationContextUtils.getWebApplicationContext(getServletContext()); 
    //wac is root context, not dispatcherServlet's context 
}

我想獲得dispatcherServlet的上下文(servlet-context.xml)。

請幫我

+0

http://stackoverflow.com/questions/21827548/spring-get-current- applicationcontext – StanislavL

回答

0

我din't測試,但你可以嘗試在春天文檔此 檢查

DispatcherServlet ds = new DispatcherServlet(request.getServletContext()) 
+0

它對我有用。謝謝 –