我在春季使用攔截器,我需要在所有控制器上實現一個攔截器,以在調用它們時處理特定的邏輯。Spring中的攔截器2.5
的web.xml:
<servlet>
<servlet-name>MyApp</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/applicationContext.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>MyApp</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
的applicationContext.xml:
<bean id="handlerMapping"
class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
<property name="interceptors">
<list>
<ref bean="warningInterceptor"/>
</list>
</property>
<property name="mappings">
<value>*.do</value>
</property>
</bean>
<bean id="warningInterceptor" class="security.WarningInterceptor">
<property name="activeApp" value="${myWarning}"/>
</bean>
Java類:WarningInterceptor
public class WarningInterceptor extends HandlerInterceptorAdapter {
private int activeApp;
public int getActiveApp() { return activeApp; }
public void setActiveApp(int activeApp) {this.activeApp = activeApp;}
public boolean preHandle(
HttpServletRequest request,
HttpServletResponse response,
Object handler) throws Exception {
if (getActiveApp() == 0) {
return true;
} else {
response.sendRedirect("/myWarning.do");
return false;
}
}
當我開始MyApp的我總是收到此錯誤:
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'handlerMapping' defined in ServletContext resource [/WEB-INF/applicationContext.xml]: Initialization of bean failed; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named '' is defined
我看不出有什麼問題。你有沒有嘗試清理和重建整個項目? – gigadot
是的,我認爲與映射中的值「* .do」有關,但我不知道會發生什麼:-( – Marta