Reputation: 11991
Starting my way with STS and created a new basic "Hello World" Spring MVC project. I wanted to add a filter to my app so I created a filter (HelloWorldFilter.java) with the following doFilter method:
public void doFilter(ServletRequest request, ServletResponse response,
FilterChain chain) throws IOException, ServletException {
System.out.println("Entering Filter");
request.setAttribute("hello", "Hello World from HelloWorldFilter!");
chain.doFilter(request, response);
System.out.println("Exiting HelloWorldFilter");
}
According to what I read it (my filter) should also be defined in the application context as a spring bean (Spring delegates it to my filter - from this manual )
So in my application context I have:
<bean id="helloWorldFilter" class="com.yl.mvc.filters.HelloWorldFilter"> </bean>
My web.xml contains the following:
<filter>
<display-name>HelloWorldFilter</display-name>
<filter-name>HelloWorldFilter</filter-name>
<filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
</filter>
<filter-mapping>
<filter-name>HelloWorldFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
In my .jsp file I added:
<P><%=request.getAttribute("hello")%></P>
But all I see in my web-page is null (I expected Hello World from HelloWorldFilter!). The filter doesn't even get invoked..
Am I missing something here?
Thanks in advance, Yogi
Upvotes: 3
Views: 3622
Reputation: 11991
Ok go it solved.
The filter (which is a spring bean) must have the same name in the bean definition (in the application context) as that in the filter-name element (in the web.xml).
In my case I had in my application context:
<bean id="helloWorldFilter"...
and in my web.xml:
<filter-name>HelloWorldFilter</filter-name>
So once it is with a capital H and once a small h - which caused the issue. To resolve it I simply changed the bean id in my application context to HelloWorldFilter.
Upvotes: 2