使用SpringMVC 3.2.1提供静态内容时遇到问题。 (Tomcat 7)
因此,请查看以下配置:
的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);
}
所以,我能够显示静态内容,如图像oder .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>
我想用
根据我的
<mvc:resources location="/WEB-INF/, classpath:/META-INF/web-resources/" mapping="/resources/**"/>
在FirstController-servlet.java文件中输入。
感谢您的帮助。 最好的问候。
答案 0 :(得分:1)
我在这里看到一些问题:
0.1。不应使用<mvc:resources
标记公开WEB-INF的内容,应保护内容。而是在WEB-INF资源下或直接在webapp下创建资源子文件夹,并为静态内容单独公开:
<mvc:resources location="/WEB-INF/resources/, /resources/, classpath:META-INF/resources/" mapping="/resources/**"/>
0.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}"/>