如何使用类路径中的位置( how to use the classpath locatio

2019-09-18 07:23发布

我已经搜查高和低,为了解决这个问题,但我不能找到一个解决方案。 问题是如下:我有一个Spring的MVC Web应用程序建立一个Tomcat 7的服务器上- 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>

文件夹结构:

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.

现在,如果我想访问旧资源文件夹,下面的代码会工作。

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

但我宁愿是做这样的事情:

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

谁能向我解释我是如何做到这一点? 我是相当新的弹簧MVC。

EDIT1:其他数据:我使用NetBeans 7.1.2和Spring MVC版本3.1.0与Maven结合。

下面是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:我添加了完整的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:一些更多的测试后,我意识到, <mvc:resources>标签不工作。 (看更新的文件夹结构)如果我,例如,做:

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

它给了我一个404为好,而实际上,它应该链接到/resources/web/css/main.css文件。 因此,所有我现在能想到的,是有一些怀念的配置。

Answer 1:

所以很多的调试和“尝试”的事情后,我试图添加的* .css和* .png格式映射到web.xml的servlet。 显然,资源文件没有映射到Servlet和为此找不到。 所以,每个人都用相同的映射,矿山,将它添加到你的web.xml 的servlet映射标签。

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


Answer 2:

这应该工作:

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

你指的是资源利用的一个URI /resources/somestatic.gif但它会从被服务WEB-INF/resources/somestatic.gif



Answer 3:

你在做什么是完全错误的。 因为你的资源所在的文件夹/ WEB-INF /网络/资源/你不需要指定类路径:之前。

因此,这将成为像下面

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

我认为这将解决您的问题。 希望这可以帮助你。 干杯。



文章来源: how to use the classpath location