简体   繁体   中英

Spring MVC Interceptor Mapping Problems

I have this segment of XML:

<mvc:interceptors>
    <mvc:interceptor>
        <mvc:mapping path="/statics/**" />
        <bean class="com.company.website.servlet.StaticsHandlerInterceptor" />
    </mvc:interceptor>
    <mvc:interceptor>
        <mvc:mapping path="/data/**" />
        <bean class="com.company.website.servlet.AJAXHandlerInterceptor" />
    </mvc:interceptor>
    <mvc:interceptor>
        <mvc:mapping path="/**" />
        <bean class="com.company.website.servlet.PageHandlerInterceptor" />
    </mvc:interceptor>
</mvc:interceptors>

I have three different interceptors for a reason, though the StaticsHandlerInterceptor is just the preHandle method returning true (for all of my static content (js, css, etc)). The second one is for AJAX requests. The third one is for actual pages. What I see happening is the statics and the AJAX interceptors being called when they are supposed to be; however, with them, the page interceptor is always being called. I only want the page interceptor to be called for pages. How do I make that happen?

mvc:interceptors now supports excluding a particular mapping. Currently it's only available in Spring 3.2.0.M2. You can find more about it at the JIRA item (that is now resolved): https://jira.springsource.org/browse/SPR-6570

Assuming you use a consistent naming scheme for your pages, use that - eg if your externally-visible page URLs end with .html , specify:

<mvc:mapping path="/**/*.html" />

It's not very RESTful to have "extensions" like that though - you might prefer to use a scheme like:

  • GET of /user/{id} = returns User object for user {id}, JSON format
  • POST to /user/{id} = updates User object from JSON object
  • GET to /user/page/{id} = returns HTML page for user {id}
  • etc etc

Then you can use a nice readable, semantic mapping like:

<mvc:mapping path="/**/page/**" />

which will work to any "depth" of URL structure.

Edit: OK so it seems that using the mvc:interceptors style of bean declaration isn't going to give you the expressiveness you need to specify exclusion by pattern rather than inclusion.

From what I can make out in this blog , using the more-verbose HandlerMapping approach will allow you to invert the match logic - you can specify what not to match on to get what you need:

<bean id="nonStaticNonDataMapper" class="org.springplugins.web.IgnoreSelectedAnnotationHandlerMapping">
<property name="order">
    <value>0</value>
</property>
<property name="urls">
    <list>
        <value>/statics/**</value>
        <value>/data/**</value>
    </list>
</property>
<property name="interceptors">
    <list>
        <bean class="com.company.website.servlet.PageHandlerInterceptor" />
    </list>
</property>

(Apologies for the formatting of the above snippet, Markdown thinks the /** is a comment :-)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM