admin管理员组

文章数量:1279212

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...

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...

Share Improve this question edited Apr 21, 2009 at 13:54 TStamper 30.4k10 gold badges68 silver badges73 bronze badges asked Apr 21, 2009 at 13:50 HenryHenry
Add a ment  | 

1 Answer 1

Reset to default 9

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.

本文标签: Spring MVC JavaScriptStack Overflow