Search code examples
jspspring-mvccontextpath

defining href for static resources coming under a context built with spring mvc


I am using Spring MVC with spring security. Problem I am facing is with building static resources paths to set in jsp files.

Suppose my app is deployed in context name ctxt

Suppose I want to include a css file in all of jsp, so I write href for it like this static/css/global.css . Now what happens, if I access following urls:

  1. localhost/ctxt/dashboard (css 200 OK - final url for css in browser becomes = localhost/ctxt/static/css/global.css)

  2. localhost/ctxt/dashboard/status (css 404 Not found - final url for css in browser becomes = localhost/ctxt/dashboard/static/css/global.css)


And if I change href of css in jsp to this /static/css/global.css. Then when I try to access these url:

  1. localhost/ctxt/dashboard (css 404 Not found - final url for css in browser becomes = localhost/static/css/global.css)

  2. localhost/ctxt/dashboard/status (css 404 Not found - final url for css in browser becomes = localhost/static/css/global.css)


Solution

  • just set resource in context definition like so

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

    and reference like so

    <link rel='stylesheet' href="<c:url value="/css/global.css"/>" type='text/css'/>