【发布时间】:2013-04-18 11:35:42
【问题描述】:
我目前正在使用这样的 xml 配置拦截器:
<mvc:interceptors>
<bean class="org.resthub.dashboard.BasicInterceptor" />
</mvc:interceptors>
但我想把它放在我的配置类中:
@Configuration
@ComponentScan("org.resthub.dashboard")
@EnableWebMvc
@EnableAspectJAutoProxy(proxyTargetClass=true)
public class WebAppConfig extends WebMvcConfigurerAdapter{
@Override
public void addInterceptors(InterceptorRegistry registry) {
registry.addInterceptor(new BasicInterceptor());
}
@Bean
public InternalResourceViewResolver setupViewResolver() {
InternalResourceViewResolver resolver = new InternalResourceViewResolver();
resolver.setPrefix("/WEB-INF/views/");
resolver.setSuffix(".jsp");
return resolver;
}
}
但它不起作用,我不知道为什么。拦截器永远不会被调用。
有什么想法吗?
谢谢
edit : 这里是 BasicInterceptor
package org.resthub.dashboard;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;
public class BasicInterceptor extends HandlerInterceptorAdapter {
@Override
public void postHandle(HttpServletRequest request,
HttpServletResponse response, Object handler,
ModelAndView modelAndView) throws Exception {
// TODO Auto-generated method stub
super.postHandle(request, response, handler, modelAndView);
System.out.println("INTERCEPTORORRR§§§§§§");
if (modelAndView != null && modelAndView.getModelMap() != null) {
...
}
}
}
【问题讨论】:
-
你验证过你的配置类真的被使用了吗?
-
是的,函数 addInterceptors 调用得很好。
-
BasicInterceptor 类中是否使用了@Intercept?
-
你的
BasicInterceptor是什么样的? -
不,我没有这个注释。我需要什么版本的 Spring MVC?我找不到它。
标签: java xml spring annotations interceptor