How to configure CSS with Spring MVC and Tiles

JazzHands picture JazzHands · Sep 6, 2011 · Viewed 7.7k times · Source

I'm using Spring 3.0.5 and Tiles 2.2.2 and I can't manage to get a stylesheet properly referenced in my app.

My stylesheet is in:

WEB-INF/static/css/styles.css

How do I declare it properly in my main Tiles JSP? I've been trying to figure this out for a couple of days now, and I've tried a number of suggestions that I've seen here and in other sites. I've tried adding

<mvc:resources ...>

to my spring config, although not all suggested solutions include this. No doubt it's very simple, but I just haven't found something that works.

Any help is very much appreciated!

Answer

Alejandro picture Alejandro · Sep 7, 2011

Hi Jazz i used to put the css files outside web-inf with the following configuration using spring mvc and some jstl but this configuration i guess is generic so i hope this "should help you" :

In the web.xml i add :

<servlet-mapping>
    <servlet-name>default</servlet-name>
    <url-pattern>*.css</url-pattern>
</servlet-mapping>

And in the page header i reference to css file with the following code :

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form"%>
<head>
    <title></title>
    <link href="${pageContext.request.contextPath}/css/global.css" rel="stylesheet" type="text/css" />
</head>

Regards..