views:

1483

answers:

1

In a Spring MVC app, what is the best approach for putting external JavaScript files in WEB-INF/ along with jsps?

/WEB-INF/spring/foo.jsp which has an include of

  <script src="foo.js"></script>.

I want foo.js in the WEB-INF/spring/ directory right beside the owning jsp?

I've noticed Spring has a ResourceSerlvet, but I'm not sure if that is the way to do this...

+2  A: 

There is no need to place the javascript files under WEB-INF. You can have a structure like the following in your WAR file:

.
js/
images/
WEB-INF/
WEB-INF/jsp

Jsp files under WEB-INF\jsp will usually appear in the main context of the application, so the following will work

<script type="text/javascript" src="js/foo.js">

If you do want to place them under WEB-INF you need to add another layer to serve them. Probably the easiest way is to use JSP include tags:

<%@include file='js/foo.js'%>

This will copy the entire contents of the file to the generated html. You could also serve the javascript file directly by using the appropriate mappings and setting the content type.

kgiannakakis