spring <mvc:resources> 如何使用类路径位置

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11120397/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 05:10:48  来源:igfitidea点击:

<mvc:resources> how to use the classpath location

springspring-mvcresourcesclasspath

提问by Jaap Oudejans

I've searched high and low in order to solve this problem, but I can't find a solution. The problem is as follows: I have an Spring-mvc webapp build on a Tomcat 7 server - 7.0.12 to be precise - and I'm having trouble getting the <mvc:resources>tag to work correct. As you will see below the <mvc:resources>works for the old resource folder, but I prefer having the resource folder in the /WEB-INF/web/directory.

MediorkoorVOICES-Web-servlet.xml:

为了解决这个问题,我搜索了高低,但我找不到解决方案。问题如下:我在 Tomcat 7 服务器上构建了一个 Spring-mvc webapp - 准确地说是 7.0.12 - 我无法让<mvc:resources>标签正常工作。正如您将在下面看到<mvc:resources>的旧资源文件夹的作品,但我更喜欢在/WEB-INF/web/目录中包含资源文件夹。

MediorkoorVOICES-Web-servlet.xml:

<?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"
   xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
      http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
      http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">        

    <context:component-scan base-package="com.oudejans.mediorkoorvoices.web" /> 

    <mvc:resources mapping="/resources/**" location="/WEB-INF/web/resources/"  />  
    <mvc:annotation-driven />

    <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="viewClass" value="org.springframework.web.servlet.view.JstlView" />
        <property name="prefix" value="/WEB-INF/web/jsp/" />
        <property name="suffix" value=".jsp" />
        <property name="order" value="1" />
    </bean>

    <bean id="handlerMapping" class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping" /> 

    <import resource="web/tiles/web-tiles.xml" />
</beans>

folder structure:

文件夹结构:

META-INF
resources (Old resources folder)
    - web
        - images
        - css
            - main.css      
WEB-INF
    - classes
    - lib
    - admin
    - shared
    - web
        - jsp
        - tiles
        - resources (New resources folder i want to access)
            - images
                - banner.png
            - js
            - etc.

Now if I wanted to access the old resource folder, the code below would work.

现在如果我想访问旧的资源文件夹,下面的代码就可以了。

<mvc:resources mapping="/resources/**" location="/resources/" />
&
<img src="${pageContext.request.contextPath}/resources/oldBanner.png" />

But what I would prefer is to do something like this:

但我更喜欢做这样的事情:

<mvc:resources mapping="/resources/**" location="classpath:/WEB-INF/web/resources/" />
&
<img src="${pageContext.request.contextPath}/resources/images/banner.png" />

Can someone explain to me how I do this? I'm fairly new to spring-mvc.

有人可以向我解释我是如何做到这一点的吗?我对 spring-mvc 相当陌生。

EDIT1: Additional data: I'm using netbeans 7.1.2 and spring-mvc version is 3.1.0 combined with maven.

EDIT1:附加数据:我使用的是 netbeans 7.1.2,spring-mvc 版本是 3.1.0 与 maven 相结合。

Below is the Pom.xml

下面是 Pom.xml

<!-- groupId config, etc. removed -->
<!-- packaging type is war -->
<properties>
    <endorsed.dir>${project.build.directory}/endorsed</endorsed.dir>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <org.springframework.version>3.1.0.RELEASE</org.springframework.version>
    <org.hibernate.version>3.5.6-Final</org.hibernate.version>
    <org.apache.tiles.version>2.2.2</org.apache.tiles.version>
    <netbeans.hint.deploy.server>Tomcat</netbeans.hint.deploy.server>
    <netbeans.hint.j2eeVersion>1.6</netbeans.hint.j2eeVersion>
</properties>

<repositories>
    <repository>
        <id>com.springsource.repository.bundles.release</id>
        <name>EBR Spring Release Repository</name>
        <url>http://repository.springsource.com/maven/bundles/release</url>
    </repository>
    <repository>
        <id>com.springsource.repository.bundles.external</id>
        <name>EBR External Release Repository</name>
        <url>http://repository.springsource.com/maven/bundles/external</url>
    </repository>    
</repositories>

<dependencies>
    <dependency>
        <groupId>commons-logging</groupId>
        <artifactId>commons-logging</artifactId>
        <version>1.1.1</version>
    </dependency>
    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-core</artifactId>
        <version>${org.hibernate.version}</version>
        <exclusions>
            <exclusion>
                <artifactId>slf4j-api</artifactId>
                <groupId>org.slf4j</groupId>
            </exclusion>
        </exclusions>
    </dependency>
    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-annotations</artifactId>
        <version>${org.hibernate.version}</version>
        <exclusions>
            <exclusion>
                <artifactId>slf4j-api</artifactId>
                <groupId>org.slf4j</groupId>
            </exclusion>
        </exclusions>
    </dependency>
    <dependency>        
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-entitymanager</artifactId>
        <version>${org.hibernate.version}</version>
        <exclusions>
            <exclusion>
                <artifactId>slf4j-api</artifactId>
                <groupId>org.slf4j</groupId>
            </exclusion>
        </exclusions>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-jdbc</artifactId>
        <version>${org.springframework.version}</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-tx</artifactId>
        <version>${org.springframework.version}</version>
    </dependency>
    <dependency>
        <groupId>org.apache.tiles</groupId>
        <artifactId>tiles-api</artifactId>
        <version>${org.apache.tiles.version}</version>
    </dependency>
    <dependency>
        <groupId>org.apache.tiles</groupId>
        <artifactId>tiles-core</artifactId>
        <version>${org.apache.tiles.version}</version>
        <exclusions>
            <exclusion>
                <artifactId>jcl-over-slf4j</artifactId>
                <groupId>org.slf4j</groupId>
            </exclusion>
        </exclusions>
    </dependency>
    <dependency>
        <groupId>org.apache.tiles</groupId>
        <artifactId>tiles-jsp</artifactId>
        <version>${org.apache.tiles.version}</version>
    </dependency>
    <dependency>
        <groupId>commons-dbcp</groupId>
        <artifactId>commons-dbcp</artifactId>
        <version>1.4</version>
    </dependency>
    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
        <version>5.1.18</version>
    </dependency>
    <dependency>
        <groupId>log4j</groupId>
        <artifactId>log4j</artifactId>
        <version>1.2.14</version>
    </dependency>
    <dependency>
        <groupId>org.slf4j</groupId>
        <artifactId>slf4j-simple</artifactId>
        <version>1.5.8</version>
    </dependency>
    <dependency>
        <groupId>javax</groupId>
        <artifactId>javaee-api</artifactId>
        <version>6.0</version>
        <scope>provided</scope>
    </dependency>        
    <dependency>
        <groupId>javax.servlet</groupId>
        <artifactId>jstl</artifactId>
        <version>1.2</version>
    </dependency>        
    <dependency>
        <groupId>javax.servlet</groupId>
        <artifactId>servlet-api</artifactId>
        <version>3.0-alpha-1</version>
    </dependency>
    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>4.10</version>
        <scope>test</scope>
    </dependency>        
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>org.springframework.core</artifactId>
        <version>${org.springframework.version}</version>
    </dependency> 
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>org.springframework.orm</artifactId>
        <version>${org.springframework.version}</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>org.springframework.web</artifactId>
        <version>${org.springframework.version}</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>org.springframework.web.servlet</artifactId>
        <version>${org.springframework.version}</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>org.springframework.test</artifactId>
        <version>${org.springframework.version}</version>
        <scope>test</scope>
    </dependency>
</dependencies>

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>2.3.2</version>
            <configuration>
                <source>1.6</source>
                <target>1.6</target>
                <compilerArguments>
                    <endorseddirs>${endorsed.dir}</endorseddirs>
                </compilerArguments>
            </configuration>
        </plugin>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-war-plugin</artifactId>
            <version>2.1.1</version>
            <configuration>
                <failOnMissingWebXml>false</failOnMissingWebXml>
            </configuration>
        </plugin>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-dependency-plugin</artifactId>
            <version>2.1</version>
            <executions>
                <execution>
                    <phase>validate</phase>
                    <goals>
                        <goal>copy</goal>
                    </goals>
                    <configuration>
                        <outputDirectory>${endorsed.dir}</outputDirectory>
                        <silent>true</silent>
                        <artifactItems>
                            <artifactItem>
                                <groupId>javax</groupId>
                                <artifactId>javaee-endorsed-api</artifactId>
                                <version>6.0</version>
                                <type>jar</type>
                            </artifactItem>
                        </artifactItems>
                    </configuration>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>

EDIT2: I added the complete MediorkoorVOICES-Web-servlet.xml and the web.xml

web.xml:

EDIT2:我添加了完整的 MediorkoorVOICES-Web-servlet.xml 和 web.xml

web.xml:

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>
            /WEB-INF/hibernateContext.xml 
            /WEB-INF/applicationContext.xml
        </param-value>
    </context-param>
    <context-param>
        <param-name>log4jConfigLocation</param-name>
        <param-value>/WEB-INF/log4j.xml</param-value>
    </context-param>    

    <listener>
        <listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>
    </listener>
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>

    <error-page>
        <error-code>404</error-code>
        <location>/errors/404.err</location>
    </error-page>

    <servlet>
        <servlet-name>MediorkoorVOICES-Web</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>    
    <servlet-mapping>
        <servlet-name>MediorkoorVOICES-Web</servlet-name>
        <url-pattern>*.mkvp</url-pattern>
    </servlet-mapping>

    <servlet>
        <servlet-name>MediorkoorVOICES-Admin</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <load-on-startup>2</load-on-startup>
    </servlet>    
    <servlet-mapping>
        <servlet-name>MediorkoorVOICES-Admin</servlet-name>
        <url-pattern>*.mkvap</url-pattern>
    </servlet-mapping>

    <servlet>
        <servlet-name>ErrorHandler</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <load-on-startup>0</load-on-startup>
    </servlet>    
    <servlet-mapping>
        <servlet-name>ErrorHandler</servlet-name>
        <url-pattern>*.err</url-pattern>
    </servlet-mapping>

    <session-config>
        <session-timeout>
            30
        </session-timeout>
    </session-config>
    <welcome-file-list>
        <welcome-file>redirect.jsp</welcome-file>
    </welcome-file-list>
</web-app>

EDIT3: After some more testing I realised that the <mvc:resources>tag isn't working at all. (Look at updated folder structure) If I, for instance, do:

EDIT3:经过更多测试后,我意识到该<mvc:resources>标签根本不起作用。(查看更新的文件夹结构)例如,如果我执行以下操作:

<mvc:annotation-driven />
<mvc:resources location="/resources/web/" mapping="/resources/**" /> 
&
<a href="${pageContext.request.contextPath}/resources/css/main.css" />click</a>

It gives me an 404 as well, while in fact, it should link to the /resources/web/css/main.css file. So all I can think of right now, is that there is some miss configuration.

它也给了我一个 404,而事实上,它应该链接到 /resources/web/css/main.css 文件。所以我现在能想到的就是有一些未命中的配置。

采纳答案by Jaap Oudejans

So after a lot of debugging and "trying" things, I tried to add the *.css and *.png mapping to the web.xml servlet. Apparently, the resource files weren't mapped to the servlet and therefor couldn't be found. So for everyone with the same mapping as mine, add this to your web.xml servlet-mappingtag.

因此,经过大量调试和“尝试”之后,我尝试将 *.css 和 *.png 映射添加到 web.xml servlet。显然,资源文件没有映射到 servlet,因此无法找到。因此,对于与我具有相同映射的每个人,请将其添加到您的 web.xml servlet-mapping标记中。

<url-pattern>*.css</url-pattern>
<url-pattern>*.png</url-pattern>

回答by Biju Kunjummen

This should work:

这应该有效:

<mvc:resources location="WEB-INF/resources/" mapping="/resources/**" />

You are referring to the resources using a uri of /resources/somestatic.gifbut it will be served from WEB-INF/resources/somestatic.gif

您指的是使用 uri 的资源,/resources/somestatic.gif但它将从WEB-INF/resources/somestatic.gif

回答by Japan Trivedi

What you are doing is totally wrong. Since your resources are in the folder /WEB-INF/web/resources/you don't need to specify classpath:before it.

你在做什么是完全错误的。由于您的资源位于文件夹/WEB-INF/web/resources/ 中,因此您无需在它之前指定classpath:。

So it would be just like below

所以就像下面一样

<mvc:resources mapping="/WEB-INF/web/resources/**" location="/WEB-INF/web/resources/"  />

I think that would resolve your problem. Hope this helps you. Cheers.

我认为这会解决你的问题。希望这对你有帮助。干杯。