Where do javascript files go in a Maven webapp archetype?

java, javascript, maven, spring-mvc

Solution

Ok so after reading the above answers and some other SO posts I have come to two solutions:

I'm using Spring MVC and my `DispatcherServlet` is mapped to root `/`. This means that any request for anything will go through it, but I have no controllers to serve resource files. What you can do is map the `DispatcherServlet` to something like `/app/*`. Then all regular urls like

`http:\\localhost:8080\ContextRoot\users`

will become accessible at

`http:\\localhost:8080\ContextRoot\app\users`.

You can then go to

`http:\\localhost:8080\ContextRoot\static\js\file.js`

and you'll be served the resource file by Apache server (or whatever you are using).

The guys at Spring probably saw this as an issue and made their own solution with `DispatcherServlet` still mapped at `/`. In your `servlet.xml` file you have to add

`<mvc:resources location = "/static/" mapping = "/static/**" />`

with your folder `static` being under folder `webapp`. This tag tells Spring MVC to have a special handler for anything going to `/static/`. You also need

`<mvc:annotation-driven>`

otherwise your Controllers handler methods will get mixed up with the handler from the previous tag and nothing will work (at least it didn't for me).

I tried both of these and they both worked but I went for the second one because my anchors in my views were mapped to root and I didn't feel like going to fix them all. Make your links relative, people!

Problem

I have a java Spring MVC web app with the following directory structure : ``` src/ main/ java/ resources/ webapp/ ``` In my webapp folder, I have a number of html files with javascript scripts. I don't want to have the javascript embedded in the html, I want it externalized. Where am I supposed to put the javascript files in the above file structure and what configuration do I have to add to my pom.xml and/or servlet-context.xml (spring) files?

Original source