2015-05-23 28 views
1

我试图在我的spring.xml配置文件中的spring应用程序中声明拦截器。我得到的错误:在spring 4应用程序中声明拦截器

Line 18 in XML document from ServletContext resource [/WEB-INF/spring/spring.xml] is invalid; nested exception is org.xml.sax.SAXParseException; lineNumber: 18; columnNumber: 16; cvc-complex-type.2.4.a: Invalid content was found starting with element 'interceptors'. 

它也不喜欢我annotation-driven标签

<beans xmlns="http://www.springframework.org/schema/beans" 
    xmlns:context="http://www.springframework.org/schema/context" 
    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-4.0.xsd 
         http://www.springframework.org/schema/context 
         http://www.springframework.org/schema/context/spring-context-4.0.xsd"> 
    <bean id="viewResolver" 
     class="org.springframework.web.servlet.view.InternalResourceViewResolver"> 
     <property name="prefix"> 
      <value>/WEB-INF/jsp/</value> 
     </property> 
     <property name="suffix"> 
      <value>.jsp</value> 
     </property> 
    </bean> 
    <context:component-scan base-package="com.app" /> 
    <interceptors> 
     <bean class="com.app.interceptors.LoginLogoutURLInterceptor" /> 
     <interceptor> 
      <bean class="com.app.interceptors.AccountServletInterceptor" /> 
      <mapping path="/account/**" /> 
     </interceptor> 
     <interceptor> 
      <bean class="com.app.interceptors.AdminServletInterceptor" /> 
      <mapping path="/admin/**" /> 
     </interceptor> 
     <interceptor> 
      <bean class="com.app.interceptors.HomePageInterceptor" /> 
      <mapping path="/" /> 
     </interceptor> 
     <interceptor> 
      <bean class="com.app.interceptors.RegistrationServletInterceptor" /> 
      <mapping path="/register/**" /> 
     </interceptor> 
    </interceptors> 
    <!-- Enables the Spring MVC @Controller programming model --> 
    <annotation-driven /> 
    <!-- Handles HTTP GET requests for /resources/** by efficiently serving 
     up static resources in the ${webappRoot}/resources directory --> 
    <resources mapping="/resources/**" location="/resources/" /> 
</beans> 

难道我宣布我拦截了正确的配置文件中?

回答

2

您必须声明为Spring MVC XML元素的名称空间在XML文件的标题:

<beans xmlns="http://www.springframework.org/schema/beans" 
    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.xsd 
     http://www.springframework.org/schema/mvc 
     http://www.springframework.org/schema/mvc/spring-mvc.xsd"> 

然后使用mvc命名为interceptors元素:

<mvc:interceptors> 
    <mvc:interceptor> 
     <!-- ... --> 
    </mvc:interceptor> 
</mvc:interceptors> 

17.16 Configuring Spring MVC在Spring参考文档中获取更多信息。

+0

谢谢,我想我有更多这样的问题。现在它说'bean'是意想不到的,预计会有'{“http://www.springframework.org/schema/mvc":mapping}'之一。 – user1154644

+0

你有'mvc:mapping'元素吗?请仔细阅读[文档](http://docs.spring.io/spring/docs/current/spring-framework-reference/htmlsingle/#mvc-config-interceptors),看看你的配置文件应该看起来像什么。 – Jesper