Spring MVC 3,拦截器都排除了一些定义的路径

mom*_*omo 45 spring-mvc interceptor

是否可以将拦截器应用于所有控制器和操作,除了一些已定义的?

为了清楚起见,我对在已定义的列表中应用拦截器不感兴趣.我想定义要排除的那些.

谢谢!

gam*_*ore 66

从Spring 3.2开始,他们在标签中添加了该功能

mvc:exclude-mapping
Run Code Online (Sandbox Code Playgroud)

从Spring文档中查看此示例:

<mvc:interceptors>
<bean class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor" />
<mvc:interceptor>
    <mvc:mapping path="/**"/>
    <mvc:exclude-mapping path="/admin/**"/>
    <bean class="org.springframework.web.servlet.theme.ThemeChangeInterceptor" />
</mvc:interceptor>
<mvc:interceptor>
    <mvc:mapping path="/secure/*"/>
    <bean class="org.example.SecurityInterceptor" />
</mvc:interceptor>
Run Code Online (Sandbox Code Playgroud)

这是doc 的链接

  • 确保xsd指向3.2.我花了十分钟试图找出问题所在.xsi:schemaLocation ="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/弹簧MVC-3.2.xsd"> (2认同)

Abd*_*han 22

对于基于java的配置,来自docs

@Configuration
@EnableWebMvc
public class WebConfig extends WebMvcConfigurerAdapter {

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(new LocaleInterceptor());
        registry.addInterceptor(new ThemeInterceptor()).addPathPatterns("/**").excludePathPatterns("/admin/**");
        registry.addInterceptor(new SecurityInterceptor()).addPathPatterns("/secure/*");
    }

}
Run Code Online (Sandbox Code Playgroud)