我尝试在 spring.xml 配置文件中的 spring 应用程序中声明拦截器。我收到以下错误:
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'.
它也不喜欢我的注释驱动
标签
<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>
我是否在正确的配置文件中声明了我的拦截器?
最佳答案
您必须在 XML 文件的 header 中声明 Spring MVC 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 引用文档中了解更多信息。
关于java - 在 Spring 4 应用程序中声明拦截器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30415367/