2

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?

Blankman
  • 259,732
  • 324
  • 769
  • 1,199
  • Have you read point 16.14.5 in http://static.springsource.org/spring/docs/current/spring-framework-reference/html/mvc.html? – madth3 Mar 27 '12 at 23:49

1 Answers1

5

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

nickdos
  • 8,348
  • 5
  • 30
  • 47
  • contextPath is what? because I wanted to keep the images relative if possible. Or even have a subdomain for the urls (say they are served from a CDN.... – Blankman Mar 28 '12 at 01:48
  • contextPath is the root path (usually app/war name) if you are running in tomcat, etc (but can be empty if running with virtual host in production environment). How are you running your app - what is the full URL to a page? – nickdos Mar 28 '12 at 02:39
  • well it might be a custom mapped domain (will have thousands of them) or a subdomain on say mywebsite.example.com – Blankman Mar 28 '12 at 14:57
  • contextPath simply lets you run the app on dev machine via tomat like `http://localhost:8080/appname/assets/images/bar.jpg` and also works when deployed in prod env like `http://foo.com/assets/images/bar.jpg` - just like `` – nickdos Mar 30 '12 at 00:01