2013-10-24 102 views
1

使用SpringMVC 3.2.1提供靜態內容時遇到問題。 (Tomcat的7)使用SpringMVC提供靜態內容

所以,請看看以下配置:

的web.xml:

<?xml version="1.0" encoding="UTF-8"?> 
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xmlns="http://java.sun.com/xml/ns/javaee" 
    xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" 
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" 
    id="WebApp_ID" version="3.0"> 
    <display-name>CrimeMapping IP5</display-name> 
    <welcome-file-list> 
     <welcome-file>index.jsp</welcome-file> 
    </welcome-file-list> 

    <servlet> 
     <servlet-name>FirstController</servlet-name> 
     <servlet-class> 
      org.springframework.web.servlet.DispatcherServlet 
     </servlet-class> 
     <load-on-startup>1</load-on-startup> 
    </servlet> 

    <servlet-mapping> 
     <servlet-name>FirstController</servlet-name> 
     <url-pattern>/</url-pattern> 
    </servlet-mapping> 
</web-app> 

FirstController-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="ch.fhnw.ip5.cm.controller" /> 


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

<!-- Allows for mapping the DispatcherServlet to "/" by forwarding 
static resource requests to the container's default Servlet --> 
<mvc:default-servlet-handler/> 


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

MapController.java:

package ch.fhnw.ip5.cm.controller; 

import javax.servlet.http.HttpServletRequest; 

import org.springframework.stereotype.Controller; 
import org.springframework.web.bind.annotation.PathVariable; 
import org.springframework.web.bind.annotation.RequestMapping; 
import org.springframework.web.bind.annotation.RequestMethod; 
import org.springframework.web.servlet.ModelAndView; 

@Controller 
public class MapController { 


    @RequestMapping(value="/show/map/lat/{lat}/lng/{lng}/zoom/{zoom}", method=RequestMethod.GET) 
    public ModelAndView showMapDetail(@PathVariable("lat") double lat, 
      @PathVariable("lng") double lng, @PathVariable("zoom") double zoom, HttpServletRequest request) { 

     request.getPathInfo(); 

     double[] message = {lat,lng,zoom}; 
     return new ModelAndView("map", "message", message); 
    } 

因此,我可以顯示靜態內容,如圖像或.js文件。 但我在.jsp文件使用的路徑是這樣的:

<link rel="stylesheet" type="text/css" href="../../../../../../../../../CM/resources/javascript/leaflet/leaflet.css" /> 
<script type="text/javascript" src="../../../../../../../../../CM/resources/javascript/leaflet/leaflet.js"></script> 

我想根據我的FirstController-servlet.java

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

入門使用

文件。

謝謝你的幫助。 此致敬禮。

回答

1

我在這裏看到幾個問題:

.1。 WEB-INF的內容不應該使用<mvc:resources標籤暴露,內容應該受到保護。相反,在WEB-INF資源下或直接在web應用下創建資源子文件夾,並單獨暴露靜態內容:

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

.2。現在,爲了引用資源,最好是以絕對方式引用它們,例如通常使用<spring:url/>或使用jstl <c:url標籤,例如。

<%@ taglib uri="http://www.springframework.org/tags" prefix="spring"%> 
<spring:url value="/resources/image.png" var="myImage" /> 

<img src="${myImage}"/> 
+0

謝謝!有用! 但我必須使用「絕對路徑」,否則我會得到相同的「未找到」錯誤。 – hallo02

相關問題