拦截器与弹簧3 - 上下文初始化失败

时间:2011-10-31 12:58:33

标签: java spring java-ee

我正在尝试在controller网址后面的/index上运行一些简单的拦截器。使用这篇文档http://static.springsource.org/spring/docs/3.0.2.RELEASE/spring-framework-reference/html/mvc.html#mvc-handlermapping-interceptor。这是我的豆子:

<bean id="handlerMapping"
    class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
    <property name="mappings">
        <value>
            /index
        </value>
    </property>
    <property name="interceptors">
        <list>
            <ref bean="myinter" />
        </list>
    </property>
</bean>

<bean id="myinter" class="my.classes.myinterceptor">
</bean>

但不幸的是我得到了:

SEVERE: Context initialization failed
org.springframework.beans.factory.BeanCreationException: Error
creating bean with name 'handlerMapping' defined in ServletContext
resource [/WEB-INF/spring-servlet.xml]: Initialization of bean failed;
nested exception is
org.springframework.beans.factory.NoSuchBeanDefinitionException: No
bean named '' is defined

感谢您的建议。

编辑,这是我的拦截器:

package my.classes;

import java.io.IOException;
import javax.servlet.http.HttpServletResponse;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

public class myinterceptor extends HandlerInterceptorAdapter{
    public boolean preHandle(HttpServletResponse response){
        try {
                    System.out.println("bla bla");
            response.sendRedirect("http://google.com");
        } catch (IOException e) {
            e.printStackTrace();
        }
        return true;
    }
}

1 个答案:

答案 0 :(得分:1)

SimpleUrlHandlerMapping的目的是将url映射到某个处理程序bean。您无法定义该处理程序bean。尝试通过bean id将url映射到上下文中的某个控制器,如下所示。

<bean id="handlerMapping"
    class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
    <property name="mappings">
        <value>
            /index=someController
        </value>
    </property>...