Search code examples
jspspring-mvcstaticresourcethymeleaf

Spring MVC 3 and handling static content - references inside templates


we're using Spring 3.1 for our webapp.

We currently map static resources with

<mvc:resources mapping="/static/**" location="/static/" />

and inside our skeleton template we refer to stylesheets like this:

<link rel="stylesheet" href="static/css/main.css">

What happens now is that I have no issues on loading css and images inside the main pages of the webapp (so like http://www.mysite.com ) but I get 404s inside inner pages (so like http://www.mysite.com/section1/chapter1 )

How can I solve this?

EDIT:

We're not using JSP/JSTL. We're using Thymeleaf.


Solution

  • According to the Themeleaf documentation, it would be something like this :

    <link rel="stylesheet" th:href="@{/static/css/main.css}">
    

    Or you can also use the JSTL c:url tag if you are using JSP :

    <%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
    
    <link rel="stylesheet" href="<c:url value="/static/css/main.css" />">