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.

3 Answers 3

2

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

Sign up to request clarification or add additional context in comments.

Comments

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?

Comments

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.