0

我有一个 Spring (3.2.2) 后端为 jQuery Mobile 前端提供静态资源。资源位于地图 /webapp/resources/images 下。此地图中已存在的资源已正确返回,但是当我添加新资源并尝试访问它(使用浏览器)时,我得到 404 not found。当我从地图中删除现有资源时,仍然可以根据请求找到它。我猜这是一些缓存问题?

web.xml

<?xml version="1.0"?>
<web-app id="WebApp_ID" version="2.4"
 xmlns="http://java.sun.com/xml/ns/j2ee"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee   http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
 <context-param>
  <param-name>contextConfigLocation</param-name>
  <param-value>/WEB-INF/mvc-dispatcher-servlet.xml</param-value>
 </context-param>
 <listener>
  <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
 </listener>
 <servlet>
  <servlet-name>mvc-dispatcher</servlet-name>
  <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
  <load-on-startup>1</load-on-startup>
 </servlet>
 <servlet-mapping>
  <servlet-name>mvc-dispatcher</servlet-name>
  <url-pattern>/</url-pattern>
 </servlet-mapping>
</web-app>

mvc-调度程序-servlet.xml

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


    <bean id="messageSource"
        class="org.springframework.context.support.ResourceBundleMessageSource">
        <property name="basenames">
            <list>
                <value>mymessages</value>
            </list>
        </property>
    </bean>

    <context:component-scan base-package="com.bvb.oiga.frontend.page.controller"/>
    <context:component-scan base-package="com.bvb.oiga.dto"/>

    <mvc:annotation-driven />
    <mvc:resources mapping="/resources/**" location="/resources/, classpath:/resources/" cache-period="0"  />

    <bean class="org.springframework.web.servlet.view.ContentNegotiatingViewResolver">
    <property name="mediaTypes">
        <map>
        <entry key="html" value="text/html"/>
        <entry key="json" value="application/json"/>
        <entry key="form" value="application/x-www-form-urlencoded"/>
        </map>
    </property>
    <property name="viewResolvers">
        <list>
        <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
                    <property name="prefix">
                        <value>/WEB-INF/pages/</value>
                    </property>
                    <property name="suffix">
                        <value>.jsp</value>
                    </property>
            </bean>
        </list>
    </property>
    <property name="defaultViews">
        <list>
        <bean class="org.springframework.web.servlet.view.json.MappingJacksonJsonView" />
        </list>
    </property>
    </bean>
</beans>

记录:

.DispatcherServlet - Bound request context to thread: org.apache.catalina.connector.RequestFacade@d15c31
.DispatcherServlet - DispatcherServlet with name 'mvc-dispatcher' processing GET request for [/oiga-frontend/resources/images/new-logo-sm.png]
.DispatcherServlet - Testing handler map [org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping@30562b] in DispatcherServlet with name 'mvc-dispatcher'
.mvc.method.annotation.RequestMappingHandlerMapping - Looking up handler method for path /resources/images/new-logo-sm.png
.mvc.method.annotation.RequestMappingHandlerMapping - Did not find handler method for [/resources/images/new-logo-sm.png]
.DispatcherServlet - Testing handler map [org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping@181ddd] in DispatcherServlet with name 'mvc-dispatcher'
.handler.BeanNameUrlHandlerMapping - No handler mapping found for [/resources/images/new-logo-sm.png]
.DispatcherServlet - Testing handler map [org.springframework.web.servlet.handler.SimpleUrlHandlerMapping@f59bb9] in DispatcherServlet with name 'mvc-dispatcher'
.handler.SimpleUrlHandlerMapping - Matching patterns for request [/resources/images/new-logo-sm.png] are [/resources/**]
.handler.SimpleUrlHandlerMapping - URI Template variables for request [/resources/images/new-logo-sm.png] are {}
.handler.SimpleUrlHandlerMapping - Mapping [/resources/images/new-logo-sm.png] to HandlerExecutionChain with handler [org.springframework.web.servlet.resource.ResourceHttpRequestHandler@1ed7b17] and 1 interceptor
.DispatcherServlet - Testing handler adapter [org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter@2c304d]
.DispatcherServlet - Testing handler adapter [org.springframework.web.servlet.mvc.HttpRequestHandlerAdapter@1172b04]
.DispatcherServlet - Last-Modified value for [/oiga-frontend/resources/images/new-logo-sm.png] is: -1
.resource.ResourceHttpRequestHandler - Trying relative path [images/new-logo-sm.png] against base location: ServletContext resource [/resources/]
.resource.ResourceHttpRequestHandler - Relative resource doesn't exist or isn't readable: ServletContext resource [/resources/images/new-logo-sm.png]
.resource.ResourceHttpRequestHandler - Trying relative path [images/new-logo-sm.png] against base location: class path resource [resources/]
.resource.ResourceHttpRequestHandler - Relative resource doesn't exist or isn't readable: class path resource [resources/images/new-logo-sm.png]
.resource.ResourceHttpRequestHandler - No matching resource found - returning 404
.DispatcherServlet - Null ModelAndView returned to DispatcherServlet with name 'mvc-dispatcher': assuming HandlerAdapter completed request handling
.DispatcherServlet - Cleared thread-bound request context: org.apache.catalina.connector.RequestFacade@d15c31
.DispatcherServlet - Successfully completed request

*更新:*

每次资源更新后,我都会重新加载(停止/启动)在 Eclipse Juno SR2 中运行的 apache-tomcat-7.0.32。还尝试关闭eclipse并重新启动,但没有任何运气。

父 Pom:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.bvb.oiga</groupId>
  <artifactId>oiga</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>pom</packaging>
  <properties>
    <spring.version>3.2.2.RELEASE</spring.version>
    <java.version>1.7</java.version>
    <log4j.version>1.2.16</log4j.version>
    <spring.ws.version>2.0.0.RELEASE</spring.ws.version>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <spring.security>3.1.3.RELEASE</spring.security>
  </properties>
  <modules>
    <module>oiga-comtrex</module>
    <module>oiga-backend-webservice</module>
    <module>oiga-dto</module>
    <module>oiga-frontend</module>
  </modules>
  <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>2.3.2</version>
                <configuration>
                    <source>${java.version}</source>
                    <target>${java.version}</target>
                </configuration>
            </plugin>
        </plugins>
  </build>
    <dependencies>
        <dependency>
            <groupId>log4j</groupId>
            <artifactId>log4j</artifactId>
            <version>${log4j.version}</version>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.8.1</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>commons-collections</groupId>
            <artifactId>commons-collections</artifactId>
            <version>3.1</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-core</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-beans</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-aop</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-aspects</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>commons-collections</groupId>
            <artifactId>commons-collections</artifactId>
            <version>3.2</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-oxm</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.ws</groupId>
            <artifactId>spring-ws-core</artifactId>
            <version>${spring.ws.version}</version>
        </dependency>
        <dependency>
            <groupId>org.apache.ws.commons.schema</groupId>
            <artifactId>XmlSchema</artifactId>
            <version>1.4.3</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>org.springframework.transaction</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>org.springframework.test</artifactId>
            <version>${spring.version}</version>
            <scope>test</scope>
        </dependency>

    </dependencies>

    <repositories>
        <repository>
            <id>com.springsource.repository.bundles.release</id>
            <name>SpringSource Enterprise Bundle Repository - SpringSource Bundle Releases</name>
            <url>http://repository.springsource.com/maven/bundles/release</url>
            <snapshots>
                <enabled>false</enabled>
                <updatePolicy>never</updatePolicy>
            </snapshots>
        </repository>

        <repository>
            <id>com.springsource.repository.bundles.external</id>
            <name>SpringSource Enterprise Bundle Repository - External Bundle Releases</name>
            <url>http://repository.springsource.com/maven/bundles/external</url>
            <snapshots>
                <enabled>false</enabled>
                <updatePolicy>never</updatePolicy>
            </snapshots>
        </repository>

        <repository>
            <id>jboss-maven2-release-repository</id>
            <url>https://repository.jboss.org/nexus/content/groups/public-jboss</url>
            <snapshots>
                <enabled>false</enabled>
                <updatePolicy>never</updatePolicy>
            </snapshots>
        </repository>

    </repositories>
</project>

和项目pom:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <parent>
    <groupId>com.bvb.oiga</groupId>
    <artifactId>oiga</artifactId>
    <version>0.0.1-SNAPSHOT</version>
  </parent>
  <artifactId>oiga-frontend</artifactId>
  <packaging>war</packaging>

     <dependencies>
        <dependency>
            <groupId>com.bvb.oiga</groupId>
            <artifactId>oiga-dto</artifactId>
            <version>0.0.1-SNAPSHOT</version>
        </dependency>
        <dependency>
            <groupId>com.bvb.oiga</groupId>
            <artifactId>oiga-comtrex</artifactId>
            <version>0.0.1-SNAPSHOT</version>
        </dependency>

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>${spring.version}</version>
        </dependency>

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-web</artifactId>
            <version>${spring.version}</version>
        </dependency>

        <dependency>
            <groupId>jstl</groupId>
            <artifactId>jstl</artifactId>
            <version>1.2</version>
        </dependency>

        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>servlet-api</artifactId>
            <version>2.5</version>
        </dependency>       

    <dependency>
            <groupId>org.codehaus.jackson</groupId>
            <artifactId>jackson-mapper-asl</artifactId>
            <version>1.7.1</version>
        </dependency>
    </dependencies>

</project>
4

2 回答 2

0

让我猜猜……您是在应用程序启动src/main/resources/修改目录的内容吗?源文件夹中的更改对正在运行的应用程序不可见,因为它使用. 文件夹甚至不在类路径上。您可以通过在目录中重复您的步骤来测试这个理论。target/classes/src/target/


如果这不是您的问题的原因,添加有关您的构建工具(Maven?)和 servlet 容器(Tomcat?Jetty?独立?嵌入式?)的信息会有所帮助。

于 2013-04-22T13:56:43.610 回答
0

它现在正在工作。@kryger 指出了我的问题(tomcat 发布而不是 resourceHttpRequestHandler)。我打开了服务器选项卡,显示项目已同步。我在 /image 映射中添加了一个资源,仍然是同步的。我注意到对第二个战争项目(用于 web 服务)的引用,该项目目前未使用,因此我删除了此引用。现在,当我添加、修改或删除资源时,它正在重新发布,甚至无需重新启动服务器。

于 2013-04-23T08:53:05.557 回答