2

我在使用 SpringMVC 3.2.1 提供静态内容时遇到问题。(雄猫 7)

所以请看一下以下配置:

网页.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>

我想用

根据我的

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

FirstController-servlet.java 文件中的条目。

谢谢您的帮助。此致。

4

1 回答 1

2

我在这里看到了一些问题:

.1。WEB-INF 的内容不应该使用<mvc:resources标签暴露,内容应该受到保护。而是在 WEB-INF 资源下或直接在 webapp 下创建一个资源子文件夹,并将其单独公开用于静态内容:

<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}"/>
于 2013-10-24T15:18:03.110 回答