3

I have read more than 5 related threads here, but could not the answer: step-by step instructions. Till now I have the STS Spring MVC template structure and try to put jquery.js somewhere in my project (unfortunately, nobody says, where it should be). So, please say:

  1. where to place jquery.js in the project structure?
  2. how to refer to this location from .jsp?
  3. any other actions needed? like maven, app config changes?

My jsp works perfectly with http://code.jquery.com/jquery-1.8.3.js, but refuses to work with local file /js/jquery-1.8.3.js. And a strange thing - when jsp can not find the script, app server complaints about it, but when it (have found?) the library, no warnings, but jquery also doesn't work.

1
  • ANSWER Thanks a lot to Alexander, JB and Eddy. The answer is: 1. create 'js' folder: source/main/webapp/js 2. put there jquery.js (I renamed it) 3. add Spring tags at the beginning of jsp: <%@ taglib prefix="spring" uri="springframework.org/tags"%> 4. in the head section add the following 2 lines: <spring:url value="/js/jquery.js" var="jqueryUrl" /> <script src="${jqueryUrl}"></script> Commented Dec 29, 2012 at 14:02

3 Answers 3

7

The JavaScript files are just resources that must be downloaded by the browser. So you put them where you want under the webapp root directory.

Suppose you put your file under /js/jquery.js (in the WebContent directory of your web project). And suppose your webapp has /myFirstWebApp as context path. This means that the root of the webapp, once the application is deployed, will be at

http://localhost:8080/myFirstWebApp/

and that your JS file will thus be at

http://localhost:8080/myFirstWebApp/js/jquery.js

To generate a URL in a webapp, you typically use the <c:url> tag:

<script src="<c:url value='/js/jquery.js'/>></script>

The c:url tag takes care of the context path:it prepends it to the absolute URLs you give it in order to halp you change the context path later.

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

2 Comments

<script type="text/javascript" src="<c:url value='/js/jquery-1.8.3.js'"></script> WARN : org.springframework.web.servlet.PageNotFound - No mapping found for HTTP request with URI [/mongo/<c:url value='/js/jquery-1.8.3.js'] in DispatcherServlet with name 'appServlet'
You need to learn how to use the JSTL. See stackoverflow.com/tags/jstl/info
1

for includes, it's a better to write

<script type="text/javascript" src="<c:url value='/js/jquery-1.8.3.min.js'/>"></script>

The js folder should be placed under WebContent folder.

Comments

0
  1. Copy the JQuery library under WebContent (like, WebContent\jquery\js\jquery-1.9.1.js is valid)

  2. In JSP:

        <%@ taglib prefix="spring" uri="http://www.springframework.org/tags"%>
          ...
        <head>
    <spring:url value="/jquery/js/jquery-1.9.1.js" var="jqueryUrl" />
    <script src="${jqueryUrl}"></script>
    
    <spring:url value="/jquery/js/jquery-ui-1.10.3.custom.js" var="jqueryJsUrl" />
    <script src="${jqueryJsUrl}"></script>
     </head>
    
  3. In spring-context.xml add:

     <mvc:resources location="/jquery/" mapping="/jquery/**" /></beans>
    

Comments

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.