使用尾部反斜杠访问时,Spring servlet无法查找资源

时间:2017-05-13 15:55:59

标签: java spring-mvc servlets

我面临一个非常奇怪的问题。我正在开发基于SpringMVC网络的应用程序,我正在将每个URL重定向到我的FrontController,这是一个DispatcherServlet。

我最近发现当我访问没有反斜杠的URL时,一切正常。例如,在本地工作,当我访问http://localhost:8080/aprv时,所有静态资源都被完美加载。 enter image description here 但是当我访问http://localhost:8080/aprv/时,静态资源似乎缺失了。 enter image description here

阅读Chrome控制台我可以看到问题所在: enter image description here

它将/ aprv / preffix添加到完整的资源路径。

所以当我使用/ aprv资源时是“/resources/template/images/feature/ENflag.jpg”

但是当使用/ aprv / resources成为“/aprv/resources/template/images/feature/ENflag.jpg /”时

为什么会这样?我怎么能解决它?

这是我的app-config.xml,其中声明了mvc:resources:

<?xml version="1.0" encoding="UTF-8"?>

<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
    xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:mongo="http://www.springframework.org/schema/data/mongo"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans.xsd
       http://www.springframework.org/schema/context
       http://www.springframework.org/schema/context/spring-context.xsd
       http://www.springframework.org/schema/mvc
       http://www.springframework.org/schema/mvc/spring-mvc.xsd
       http://www.springframework.org/schema/data/mongo
       http://www.springframework.org/schema/data/mongo/spring-mongo.xsd">

    <bean id="authenticator" class="es.unileon.ulebankoffice.domain.Authenticator"
        scope="singleton">
    </bean>

    <bean id="handlerMapping"
        class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping">
        <property name="alwaysUseFullPath" value="true"></property>
    </bean>

    <bean id="datastore" class="es.unileon.ulebankoffice.domain.Datastore"
        scope="singleton">
    </bean>

    <bean id="messageSource"
        class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
        <property name="basename" value="classpath:messages" />
        <property name="fileEncodings" value="UTF-8" />
        <property name="defaultEncoding" value="UTF-8" />
    </bean>

    <!-- Scans the classpath of this application for @Components to deploy as 
        beans -->
    <context:component-scan base-package="es.unileon.ulebankoffice.web" />
    <context:component-scan base-package="es.unileon.ulebankoffice.security" />

    <mvc:annotation-driven />

    <mvc:default-servlet-handler />

    <bean id="viewResolver"
        class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="viewClass"
            value="org.springframework.web.servlet.view.JstlView"></property>
        <property name="prefix" value="/WEB-INF/views/"></property>
        <property name="suffix" value=".jsp"></property>
    </bean>
    <bean id="localeResolver"
        class="org.springframework.web.servlet.i18n.CookieLocaleResolver">
        <property name="defaultLocale" value="es" />
        <property name="cookieName" value="ULeBankLanguagePreference"></property>
        <property name="cookieMaxAge" value="604800"></property>
    </bean>
    <mvc:resources mapping="/resources/**" location="/resources/" />
    <mvc:resources mapping="resources/**" location="/resources/" />

    <mvc:interceptors>
        <bean id="localeChangeInterceptor"
            class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor">
            <property name="paramName" value="language" />
        </bean>
    </mvc:interceptors>
    <mongo:template db-factory-ref="mongoDBFactory"
        write-concern="FSYNC_SAFE" />

    <mongo:db-factory id="mongoDBFactory" dbname="ulebankoffice"
        mongo-ref="mongoClient" />

    <mongo:mongo-client id="mongoClient"
        credentials="++++">
        <mongo:client-options connect-timeout="5000" />
    </mongo:mongo-client>

    <mongo:repositories base-package="es.unileon.ulebankoffice.repository" />
</beans>

这是我的web.xml:

<?xml version="1.0" encoding="utf-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
    version="2.5">

    <!-- location of log4j config file -->
    <context-param>
        <param-name>log4jConfigLocation</param-name>
        <param-value>/WEB-INF/logging.properties</param-value>
    </context-param>

    <!-- applies log4j configuration -->
    <listener>
        <listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>
    </listener>
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:app-config.xml,classpath:security-config.xml</param-value>
    </context-param>

    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
    <servlet>
        <servlet-name>inicio</servlet-name>
        <jsp-file>/index.jsp</jsp-file>
    </servlet>
    <filter>
        <filter-name>springSecurityFilterChain</filter-name>
        <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
    </filter>

    <filter-mapping>
        <filter-name>springSecurityFilterChain</filter-name>
        <url-pattern>/o/*</url-pattern>
    </filter-mapping>

    <servlet>
        <servlet-name>FrontalController</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>classpath:app-config.xml</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>

    <servlet-mapping>
        <servlet-name>FrontalController</servlet-name>
        <url-pattern>/*.htm</url-pattern>
        <url-pattern>/</url-pattern>
    </servlet-mapping>

    <welcome-file-list>
        <welcome-file>inicio</welcome-file>
    </welcome-file-list>

    <jsp-config>
        <jsp-property-group>
            <url-pattern>*.jsp</url-pattern>
            <page-encoding>UTF-8</page-encoding>
        </jsp-property-group>
    </jsp-config>
</web-app>

谢谢你的帮助。

1 个答案:

答案 0 :(得分:0)

正如@JBNizet在评论中提到的那样,这是一个相对路径问题。

我将"resources/.../..."更改为"/resources/.../..."后一切正常。

谢谢!