1

I just started with Spring 3 MVC today. Running into a dilemma... web.xml maps everything ("/") to Spring. But as a result, when I put something like: <link rel="stylesheet" type="text/css" href="<%=request.getContextPath()%>/css/navigation.css" />

It is not returned by the container...

Perhaps someone could suggest how to handle this?

Thanks.

R.V.
  • 532
  • 1
  • 6
  • 21

3 Answers3

2

How are you trying to serve it? If you are trying to serve it from the webapp itself (ie WEB-INF/static/css) You would need to include a servlet to do that for you. In the spring context you can include something like

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

You can see more here

How to handle static content in Spring MVC?

Community
  • 1
  • 1
John Vint
  • 39,695
  • 7
  • 78
  • 108
2

Use mvc:resources, as explained in the documentation. This allows service static resources from the web app, but also from the classpath.

JB Nizet
  • 678,734
  • 91
  • 1,224
  • 1,255
1

As suggested by others, use mvc:resource to serve your static resources.

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

It is also recommended to avoid using scriptlets in your JSP code if possible. You should instead use JSTL to build the correct path to your CSS file.

<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
...
<link rel="stylesheet" type="text/css" href="<c:url value="/resources/css/navigation.css" />"/>
Beau Grantham
  • 3,435
  • 5
  • 33
  • 43