请帮助一个Spring MVC新手。我很难让HandlerInterceptorAdapter
工作。
这是代码和配置文件。
MVC-调度-servlet.xml中:
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">
<mvc:annotation-driven />
<context:component-scan base-package="com.myproject" />
<bean class="org.springframework.web.servlet.mvc.support.ControllerClassNameHandlerMapping" >
<property name="interceptors">
<list>
<ref bean="loginInterceptor" />
</list>
</property>
</bean>
<bean id="loginInterceptor" class="com.myproject.util.login.LoginInterceptor" />
<bean id="viewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver" >
<property name="prefix">
<value>/WEB-INF/pages/</value>
</property>
<property name="suffix">
<value>.jsp</value>
</property>
</bean>
LoginInterceptor.java:
package com.myproject.util.login;
import org.apache.log4j.Logger;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class LoginInterceptor extends HandlerInterceptorAdapter {
private static final Logger LOG = Logger.getLogger(LoginInterceptor.class);
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
LOG.debug("intercepted");
return super.preHandle(request, response, handler);
}
}
我正在尝试为任何控制器运行此拦截器。也许,ControllerClassNameHandlerMapping
不是这个目的的最佳选择。请帮忙。
答案 0 :(得分:4)
<mvc:interceptors>
<mvc:interceptor>
<mvc:mapping path="/somepath/**" />
<bean class="com.mydomain.myapp.SomeInterceptor" />
</mvc:interceptor>
</mvc:interceptors>
我发现“mvc:interceptors”代替了XML bean - 请参阅:Spring MVC: Why does this <mvc:interceptors> declaration work, but not the traditional XML?
答案 1 :(得分:-1)
尝试使用:
<bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping">
而不是:
<bean class="org.springframework.web.servlet.mvc.support.ControllerClassNameHandlerMapping">