2012-11-14 112 views
6

我对Spring很新,我在Spring MVC应用程序中访问我的资源时遇到问题。我试过谷歌和堆栈溢出找到答案(这些都帮助我possible solution 1,或possible solutionpossible solution 3,spring framework),但我没有发现没有解决我的问题或提高我对问题的理解(它可能很简单因为我不太了解上述解决方案)。在Spring MVC应用程序的JSP页面中访问资源

我已经成功地嵌入我的CSS文件到一个网页,但使用的代码:

<%@include file="css/style.css" %> 

但使用URL的原因很明显这种方式我不能访问它。我已经用我的web.xml文件下面,但它没有任何效果也试过,我承认我真的不明白在这种情况下映射,这样可能是一个问题

<mvc:annotation-driven /> 
<mvc:resources mapping="/images/**" location="/images/" /> 
<mvc:resources mapping="/css/**" location="/css/" /> 

我一直在使用也尝试每个下列分别:

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

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

这是我的项目布局:

My Project structure

这是我的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> 

这是我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> 

以防万一,这里是我的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> 

我会很高兴,如果我能访问图像和css文件夹以及webapp/WEB-INF /页面中的文件夹内容与我可以访问我的jsp页面(即通过URL)的方式相同,但理想情况下,我想了解我应该如何正确执行此操作,即我应该映射文件夹(src \ main \ resources o r src \ main \ webapp \ WEB-INF \ some_resource_folder)以及我应该如何映射它。

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

回答

12

您应该将所有静态网页内容(如图像,css和javascript)放在webcontent(根目录)目录下的资源目录中。

enter image description here

然后在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/" /> 

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

<link href="${pageContext.servletContext.contextPath}/resources/My_CSS.css" rel="stylesheet"/> 
+0

您是先生吨,这工作完美,我显然只是不明白资源文件夹应该位于何处。非常感激。我有一个后续问题,我现在在我的jsp页面上出现eclipse中的错误 'javax.servlet.jsp.JspException无法解析为类型\t index.jsp'有什么建议吗? – jonnie

+1

很高兴我能帮到你,你能指出我的后续问题吗?我想回答你的更多问题。这是早上的一些好评。要回答你的问题,我需要看你的控制器。 –

+0

我现在在我的jsp页面的eclipse中出现错误javax.servlet.jsp.JspException无法解析为类型index.jsp的任何建议? – jonnie

0

当使用这样的:

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

使用此代码在JSP中:

<link href="<c:url value="/"/>resources/My_CSS.css" rel="stylesheet"/> 

看的差异。 CAPS RESOURCES查找文件路径。 和资源在网络路径位于

0

设置资源顺序相反混合JSTL和JSP EL下面

<mvc:resources mapping="/resources/**" location="/resources/" order="-1"/> 
0

,你可以这样配置Spring:

<mvc:resources mapping="/resources/**" location="/WEB-INF/pages/" /> 
你的情况

,在JSP中使用以下内容:

<link href="<c:url value="/resources/My_CSS.css"/>" rel="stylesheet"/> 
+0

我应该在哪里导入mvc标签? –

0

在你的jsp文件中y OU应该设置,

<c:set var="context" value="${pageContext.request.contextPath}" /> 

和资源文件JS,CSS,IMG等包括他们,

<script src="${context}/resources/your.file.js" type="text/javascript"></script> 
<link href="${context}/resources/your.min.css" rel="stylesheet" type="text/css"/> 

你也应该HREF到控制器链接之前使用这个,像

<a href="${context}/yourPage"> 

以这种方式,您可以使用在servlet容器或应用服务器Tomcat,jetty等中定义的上下文路径。

相关问题