timepast
V2EX  ›  Java

由于项目的需要,SpringMVC 需要同时拦截 / 和 *.html 该如何实现呢?

  •  
  •   timepast ·
    idyllim · Sep 12, 2014 · 3914 views
    This topic created in 4450 days ago, the information mentioned may be changed or developed.
    5 replies  •  2014-09-15 09:01:57 +08:00
    cxshun
    cxshun
    Sep 12, 2014
    1
    拦截/不就包含了*.html了,有点矛盾哦。
    如果你非要区分开,那就写多个url-mapping,同时为DispatcherServlet进行拦截就行了。
    timepast
    timepast
    OP
    Sep 12, 2014
    2
    @cxshun 我测试了,使用多个mapping 是不行的 如果是 .html 和 .xhtml 是可以的 。
    cxshun
    cxshun
    Sep 13, 2014
    3
    实际上是因为所有请求都被/拦截了,所以*.html不会生效的,你要做这样的需求,可能只能static resource这个做了,看一下spring的文档: http://docs.spring.io/spring/docs/3.2.11.RELEASE/spring-framework-reference/htmlsingle/#mvc-config-static-resources,可以通过mvc:resource来实现,但这也仅限于你当前的例子,因为刚才html是静态文件,如果是可以处理/和*.jsp,这又是需要另外一种处理方式了。
    实际上,如果你需要映射多个,一般建议你写多个,否则就还是做相应的修改,适应一个就行了。
    timepast
    timepast
    OP
    Sep 13, 2014
    4
    @cxshun / 不是拦截所有的请求吧 /* 才是的

    / 和 *.html 的情况下 我发现 / 不会生效 *.html 却生效了
    cxshun
    cxshun
    Sep 15, 2014
    5
    @timepast 具体的拦截匹配我就不大记得了,你可以看看我发的文档,里面有resource相关的配置,试下看行不。
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Privacy   ·   Solana   ·   2738 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 25ms · UTC 10:32 · PVG 18:32 · LAX 02:32 · JFK 05:32
    ♥ Do have faith in what you're doing.