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

标签 java spring spring-mvc

我尝试在 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/

相关文章:

java - 在 iOS 上信任我自己的 CA

java - 我们可以在 Spring Webflux 中使用 web servlet 和 servlet 过滤器吗?

java - 注释是一种 AOP 吗?

java - 使用 Java 锁时出现竞争条件的可能性

java - Spring Security 拦截 url 似乎被跳过/忽略

json - @JsonTypeInfo 可以与集合一起使用吗?

java - 用于 GET url 参数的 SpringMVC RequestMapping

java - 在 Spring Session 中禁用多 session 支持

java - 在MySQL中,如何获取存储过程的第一个输入参数的参数名称?

java - 如何将数据从 thymeleaf HTML 页面发送到 MVC spring boot Controller ?