user1755645
user1755645

Reputation: 945

Spring MVC Resources

I am trying to link my static elements in with Spring MVC using the resources tag - what I am not able to figure out is where to place them.

I am publishing my links as -

<link rel="stylesheet" href="/css/elements.css">

I have placed the files under - WebContent/resources/css/elements.css

The resources tag I am using in my servlet is -

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

Am i missing something?

Upvotes: 0

Views: 6785

Answers (4)

djangofan
djangofan

Reputation: 29689

If you are using locations relative to root context of Tomcat, then you need to do it like this. The difference between this and the other answers is that I am including the application context in the location mapping:

<mvc:resources location="/webapp-name/resources/" mapping="/resources/**"/>

The attribute "mapping" is relative to classpath whereas "location" is relative to root of Tomcat. (NOTE: these are not 'relative' urls)

This config will provide access to resources in sub-folders, such as /resources/css/*

Upvotes: 0

Jeevan Patil
Jeevan Patil

Reputation: 6089

You have your resources in "resources" folder, so you added following line in dispatcher xml.

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

And this resources folder must have been in "webcontent" folder. Then you need to access the resources by using context path.

<link rel="stylesheet" href="${pageContext.request.contextPath}/resources/css/elements.css">

Give this a try. It should work.

Upvotes: 2

user1755645
user1755645

Reputation: 945

Found the mistake .. using c:url to publish the absolute path helped :)

I am now publishing my URLs as

&lt;link rel="stylesheet" href="&lt;c:url value="/css/slideshow.css" />">

Upvotes: 0

JB Nizet
JB Nizet

Reputation: 692161

You inverted the two attributes. locations is where the files are in the web app root. mapping is the url-pattern used to access the resources from the browser. So the configuration should be

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

As explained in the documentation.

Upvotes: 2

Related Questions