Where do CSS and JavaScript files go in a Maven web app project?

Solution 1:

So if you have your DispatcherServlet configured in a REST like URL pattern such as / then css files would go under src/main/webapp/resources

Just to clarify this is what I had to do:

  1. Make sure that in your servlet-context.xml you have as follows:

    <resources mapping="/resources/**" location="/resources/" /> 
    
  2. Create a folder if does not already exist under webapps called resources

  3. Place your css folder along with css files there

  4. Reference my css file as follows:

    <link rel="stylesheet" href="<%=request.getContextPath()%>/resources/css/960.css"/>
    

Solution 2:

I was faced with the same question in a maven project. There seems to be no good answer to my problem. So I did a simple experiment and it worked perfectly.

This is a simple maven project, I have the following directory structure: (Irrelevant directories are not shown)

───src
   └───main
       ├───java
       ├───resources
       └───webapp
           ├───javascript
           │   ├───test.js
           │   ├───example.js
           │   └───jquery-library.js
           ├───WEB-INF
           │    └───web.xml
           └───example.jsp

My JSP pages can refer to my javascript libraries simply as

<script src=javascript/jquery-library.js></script>

There is no need for any special configuration.

Solution 3:

org.springframework.web.servlet.PageNotFound - No mapping found for HTTP request with URI [/appname/css/myStyles.css] in DispatcherServlet

This indicates a configuration problem with your web.xml/spring context file. DispatcherServlet should not be processing a request for a css resource.

If this does not help troubleshoot the problem, you may want to post relevant snippets of your web.xml and spring context file.