2013-10-24 2 views
1

SpringMVC 3.2.1을 사용하여 정적 컨텐트를 제공하는 데 문제가 있습니다. (톰캣 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); 
    } 

그래서 이미지 나 oj 파일과 같은 정적 인 내용을 보여줄 수 있습니다. 는하지만 .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 자원에서 직접 웹 애플리케이션 아래에 자원 하위 폴더를 생성하고 정적 콘텐츠를 혼자 노출 :

<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