hallo02
hallo02

Reputation: 327

Providing static content with SpringMVC

I have a problem with providing static content using SpringMVC 3.2.1. (Tomcat 7)

So please have a look at the following configurations:

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);
    }

So, I am able to show static content like images oder .js files. But the path I use in .jsp files looks like:

<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>

I would like to use

according to my

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

entry in FirstController-servlet.java file.

Thank you for your help. Best regards.

Upvotes: 2

Views: 565

Answers (1)

Biju Kunjummen
Biju Kunjummen

Reputation: 49935

There are a few issues that I see here:

.1. The contents of WEB-INF should not be exposed using <mvc:resources tag, the content should be protected. Instead create a resources subfolder under WEB-INF resources or directly under the webapp and expose that alone for static content:

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

.2. Now, in order to refer to the resources, it is better to refer to them in an absolute way, this is typically using <spring:url/> or using jstl <c:url tags, for eg.

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

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

Upvotes: 2

Related Questions