spring3 拦截器 - 上下文初始化失败
我正在尝试在 /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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
SimpleUrlHandlerMapping 的用途 是将 url 映射到某个处理程序 bean。您未能定义该处理程序 bean。尝试通过 bean id 将 url 映射到上下文中的某个控制器,如下所示。
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.