Spring MVC拦截器映射问题 [英] Spring MVC Interceptor Mapping Problems

查看:127
本文介绍了Spring MVC拦截器映射问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这段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>

我有三种截然不同的原因,不过 StaticsHandlerInterceptor 只是 preHandle 方法返回 true (对于我的所有静态内容(js,css等) ))。第二个是针对AJAX请求。第三个是实际页面。我所看到的是静态和AJAX拦截器被调用时应该被调用;然而,有了它们,总是会调用页面拦截器。我只希望为页面调用页面拦截器。我如何实现这一目标?

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?

推荐答案

假设您为页面使用一致的命名方案,请使用 - 例如如果您的外部可见页面网址以 .html 结尾,请指定:

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

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

虽然这样的扩展并不是非常RESTful - 您可能更喜欢使用像:

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


  • 获取 / user / {id} =返回用户的用户对象{id},JSON格式

  • POST到 / user / {id} =从 JSON对象更新用户对象

  • GET到 / user / page / {id} =为用户{返回 HTML页面 id}

  • 等等

  • 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/**" />

这将适用于任何深度的URL结构。

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

编辑:好的所以好像好像使用 mvc:interceptors bean声明样式不会为你提供你需要用模式指定排除的表现力而不是

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.

我可以在此博客中看到,使用更详细的 HandlerMapping 方法将允许您反转匹配逻辑 - 您可以指定匹配得到你需要的东西:

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>

(道歉上面代码片段的格式,Markdown认为 / ** 是评论: - )

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

这篇关于Spring MVC拦截器映射问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆