spring3 拦截器 - 上下文初始化失败

发布于 2024-12-12 17:39:45 字数 1851 浏览 0 评论 0原文

我正在尝试在 /index url 后面的 controller 上运行一些简单的拦截器。使用这篇文档 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;
    }
}

I am trying to run some simple interceptor on controller behind /index url. Usisng this piece of docs http://static.springsource.org/spring/docs/3.0.2.RELEASE/spring-framework-reference/html/mvc.html#mvc-handlermapping-interceptor. Here is my bean:

<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>

but unfortunattelly I got:

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

Thanks in advice.

edit, here is my interceptor:

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;
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

耳钉梦 2024-12-19 17:39:45

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>...

The purpose of the SimpleUrlHandlerMapping is to map a url to some handler bean. You are failing to define that handler bean. Try mapping the url to some controller in your context by bean id like below.

<bean id="handlerMapping"
    class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
    <property name="mappings">
        <value>
            /index=someController
        </value>
    </property>...
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文