Permit CSS, JS and other resources in a Spring application

In Spring 3 Web MVC applications every request is sent through an @Controller method.

This is undesirable for CSS, JS and image files. Overcome the problem as so.

In the application context , e.g. applicationContext.xml, add this

So, in this example the css files are located in /WEB-INF/css

but

they are accessed as so from path css/…

 

Comments always welcome…

 

5 Comments

  • This worked for me as well, as long as I didn’t use the “import org.springframework.web.bind.annotation.PathVariable;” in the controller. When I did that, using the above code there was an extra /person/Mary being used to figure out where the CSS file was.

    I used the following method to remove the problem:

    It seems to work. Is there a better way of doing this.

    BTW: You’re tutorials are helping me out a lot more that anything I’ve run across so far. <> keep up the good work.

    • Thanks Bob

      Yep, what you suggest is exactly what I’d recommend.
      It’s good practice to always use the {cp}/ rather than using relative paths.

      Thanks for pointing this out.