访问Spring MVC app的JSP页面中的资源

jon*_*nie 6 java spring jsp spring-mvc

我是春天的新手,我在Spring mvc应用程序中访问我的资源时遇到问题.我已经尝试了Google并使用堆栈溢出来找到答案(这些都没有帮助我解决方案1,或者可能的解决方案,可能的解决方案3,弹簧框架)但我发现没有任何一个解决了我的问题或者提高了我对问题(可能只是因为我不太明白提到的解决方案).

我已设法将我的css文件嵌入到网页中但使用代码:

<%@include file="css/style.css" %>
Run Code Online (Sandbox Code Playgroud)

但由于显而易见的原因,我无法使用这种方式访问​​它.我也尝试在我的web.xml文件中使用以下内容但它没有任何效果,我承认我在这种情况下并不真正理解映射,因此可能是一个问题

<mvc:annotation-driven />
<mvc:resources mapping="/images/**" location="/images/" />
<mvc:resources mapping="/css/**" location="/css/" />
Run Code Online (Sandbox Code Playgroud)

我也尝试分别使用以下各项:

<img src="<%=request.getContextPath()%>/images/logo.jpg"/>

<img src="<%=request.getContextPath()%>/src/main/resources/images/logo.jpg"/>
Run Code Online (Sandbox Code Playgroud)

这是我的项目布局:

我的项目结构

这是我的web.xml:

<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">
  <display-name>MyProject Application</display-name>
  <servlet>
    <servlet-name>myServlet</servlet-name>
        <servlet-class>
                  org.springframework.web.servlet.DispatcherServlet
        </servlet-class>
        <load-on-startup>1</load-on-startup>
  </servlet>
  <servlet-mapping>
    <servlet-name>myServlet</servlet-name>
        <url-pattern>/</url-pattern>
  </servlet-mapping>
</web-app>
Run Code Online (Sandbox Code Playgroud)

这是我的myServlet-servlet.xml:

<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    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-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">
    <!-- -->
     <mvc:annotation-driven />
    <mvc:resources mapping="/images/**" location="/images/" />
    <mvc:resources mapping="/css/**" location="/css/" />
    <bean name="/index.html" 
    class="com.myproject.controller.AdminController" />
    <bean id="viewResolver"
        class="org.springframework.web.servlet.view.InternalResourceViewResolver" >
        <property name="prefix">
            <value>/WEB-INF/pages/</value>
        </property>
        <property name="suffix">
            <value>.jsp</value>
        </property>
    </bean>
</beans>
Run Code Online (Sandbox Code Playgroud)

以防万一这是我的pom.xml:

<?xml version="1.0" encoding="UTF-8"?>
<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/maven-v4_0_0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.myproject</groupId>
    <artifactId>myProject</artifactId>
    <packaging>war</packaging>
    <version>0.0.1-SNAPSHOT</version>
    <name>myProject Maven Webapp</name>
    <url>http://maven.apache.org</url>
    <properties>
        <springVersion>3.0.4.RELEASE</springVersion>
    </properties>
    <dependencies>

        <!-- JUnit -->
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>3.8.1</version>
            <scope>test</scope>
        </dependency>

        <!-- ORACLE JDBC driver, need install yourself -->
        <dependency>
            <groupId>com.oracle</groupId>
            <artifactId>ojdbc6</artifactId>
            <version>11.2.0</version>
        </dependency>
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-core</artifactId>
            <version>3.6.3.Final</version>
        </dependency>

        <!-- Spring framework -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-core</artifactId>
            <version>${springVersion}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-web</artifactId>
            <version>${springVersion}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>${springVersion}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>${springVersion}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-orm</artifactId>
            <version>${springVersion}</version>
        </dependency>

        <!-- for compile only, your container should have this -->
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>servlet-api</artifactId>
            <version>2.5</version>
        </dependency>
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>jstl</artifactId>
            <version>1.2</version>
        </dependency>
        <!-- Commons-logging & log4j -->
        <dependency>
            <groupId>log4j</groupId>
            <artifactId>log4j</artifactId>
            <version>1.2.12</version>
        </dependency>
        <dependency>
            <groupId>commons-logging</groupId>
            <artifactId>commons-logging</artifactId>
            <version>1.1.1</version>
        </dependency>
    </dependencies>

    <build>
        <finalName>admin_UI</finalName>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-war-plugin</artifactId>
                <version>2.3</version>
                <configuration>
                    <webappDirectory>/sample/servlet/container/deploy/directory</webappDirectory>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>
Run Code Online (Sandbox Code Playgroud)

如果我可以访问webapp/WEB-INF/pages中的那些文件夹,就像我可以访问我的jsp页面一样(即通过URL),我会很高兴,但理想情况下我想了解我应该如何正确地做到这一点,即我应该映射的文件夹(src\main\resources或src\main\webapp\WEB-INF\some_resource_folder)以及我应该如何映射它.

如果需要更多信息,我很乐意提供.

Kev*_*sox 12

您应该将所有静态Web内容(如images,css和javascript)放在webcontent(根目录)目录下的资源目录中.

在此输入图像描述

然后在myServlet-servlet.xml将目录指定为资源目录.这将告诉调度员不要处理静态资源的请求.

<!-- Handles HTTP GET requests for /resources/** by efficiently serving up static resources in the ${webappRoot}/resources directory -->
<mvc:resources mapping="/resources/**" location="/resources/" />
Run Code Online (Sandbox Code Playgroud)

在您的jsp文件中,您应该使用依赖于JSP EL解析的上下文路径的根相对URL来访问这些资源.

<link href="${pageContext.servletContext.contextPath}/resources/My_CSS.css" rel="stylesheet"/>
Run Code Online (Sandbox Code Playgroud)