问题
Spring MVC uses a DispatcherServlet
to route control to an appropriate Controller. But where do filters fit into the flow? If I specify a filter to perform session management or authentication, will the filters always be called before DispatcherServlet?
My confusion comes from the fact that they both specify a URL pattern. What happens if they both provide the same url pattern?
回答1:
This is not really specific to Spring and Spring MVC. In general fitlers are always called before servlets. When you have several filters and one servlet matching given URL pattern, all filters are executed first in the order of <filter-mapping>
definitions and the servlet is executed last.
This way you can modify the request on the fly or even ignore the servlet altogether.
来源:https://stackoverflow.com/questions/11342010/relationship-between-spring-dispatcherservlet-and-filters