Blankman
Blankman

Reputation: 267320

Trying to get mvc resources to serve my static resources

I'm developing locally using both jetty and tomcat.

My images, css, javascript files are in:

/src/main/webapp/assets

where the folder assets has:

/src/main/webapp//assets/images
/src/main/webapp//assets/css
/src/main/webapp//assets/images/

My spring config file has:

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

I'm confused as to what both mapping and location mean?

I think mapping means that that spring will only try and serve the static files if it has the url with the pattern like:

www.example.com/assets/

What does location do?

My html currently has:

src="/assets/images/logo.gif"

I've tried playing with the location value, and I don't get to render the image for some reason.

Can someone clear this up for me?

Upvotes: 2

Views: 2464

Answers (1)

nickdos
nickdos

Reputation: 8414

If your project structure has /src/main/webapp/assets/images, then you want to use:

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

and then in your JSP reference files as

src="${pageContext.request.contextPath}/assets/images/logo.gif"

Its more common to have a project structure like /src/main/webapp/images|css|js and then use:

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

but still keeping URLs as ${pageContext.request.contextPath}/assets/images/logo.gif

Upvotes: 5

Related Questions