繁体   English   中英

在Spring 4应用程序中声明拦截器

[英]Declaring interceptors in a spring 4 application

我试图在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'.

它还不喜欢我的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>

我是否在正确的配置文件中声明了拦截器?

您必须在XML文件的标题中声明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>

有关更多信息,请参见Spring参考文档中的17.16配置Spring MVC

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM