2017-04-21 122 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

它对我有用。谢谢 –