0

I am trying to include jquery in a spring mvc app but am having some trouble (I am new to spring mvc). I created a folder called js under the webapp folder, but am not sure what to include my view to include the file.

I've tried:

<script type="text/javascript" src="${pageContext.request.contextPath}/js/jquery-2.0.3.min.js"></script>

however, this creates a path that cannot be resolved since I have no controller for /js path.

My question is how is this usually done, is a controller created that returns the jquery-2.0.3.min.js file?

7
  • " no controller for /js path" why? Commented Nov 5, 2013 at 22:13
  • I just haven't created one, but I guess that is a solution. Commented Nov 5, 2013 at 22:17
  • You have to. That is how every project works, unless you go with those central repositroies (like Mike answered) Commented Nov 5, 2013 at 22:21
  • Do you have JS in project? No Controller (or) control? If you do have JS in project, then web.xml entries should be enough, you don't need Controller. Commented Nov 5, 2013 at 22:36
  • @Nambari, not sure I follow...sorry I am a bit new. Commented Nov 5, 2013 at 22:45

3 Answers 3

1

Although you could link to a CDN for common libraries such as jquery or bootstrap, most applications eventually will need some custom CSS, JS, or images. Instead of writing a controller, you should be serving the resources via Spring MVC's ResourceServlet. You can add the following to your webmvc-config.xml (or default-servlet.xml or whatever spring mvc configuration file you have):

<!-- Handles HTTP GET requests for /resources/** by efficiently serving up static resources -->
<mvc:resources location="/" mapping="/resources/**"/>

Then, you could update your view to include "resources" in the path, such as:

<spring:url value="/resources/js/jquery-2.0.3.min.js" var="jquery_js_url" />
<script src="${jquery_js_url}" type="text/javascript"><!-- /required for FF3 and Opera --></script>

For more information, see the following:

Hope that helps...

Sign up to request clarification or add additional context in comments.

Comments

0

I have this line in my JSPs, and it works. We do not use a CDN - it is hosted locally in the javascript folder. That folder hangs under the webapps parent.

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<script src='<c:url value="/javascript/jquery.js"/>'></script>

2 Comments

I tried adding when I look at the source of the file, I just see <script src='<c:url value="/js/jquery.js"/>'></script> which does not seem to work.
additionally, I get this warning: WARRNING: No mapping found for HTTP request with URI [/<c:url value="/js/jquery.js"/>] in DispatcherServlet with name 'mvc-dispatcher'
0

I would recommend just linking to a CDN (such as Google) -- as it enables jquery to be loaded from a user's cache, rather than your servers:

https://developers.google.com/speed/libraries/devguide#jquery

5 Comments

What is the difference between getting loaded from OP server and Google server?
@Nambari -- the difference is that if a user has loaded it previously (from another website), it will be in their cache -- meaning it will load instantly, rather than waiting to download then run.
@mrkb80 -- In that case, why not just hardcode it to your js folder? Including a script like this is not part of Spring. Also, Spring might already require/load in jQuery (I'm not sure, haven't used that particular framework).
I see, make sense. But few organizations/projects restrict.
I would like to avoid hardcoding as well.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.