Samo
Samo

Reputation: 8240

How to include the jQuery library in a Spring-MVC Eclipse project

Good day,

I have searched far and wide but all I can find is information about how to add various plugins to Eclipse. I don't want a plugin. I want my stinkin' jQuery!

Some things I've tried:

I download jQuery and put it in my WebContent\WEB-INF\js folder. Then in my WebContent\WEB-INF\jsps\company.jsp file, I have a script tag:

<script type="text/javascript" src="../js/jquery-1.4.3.min.js"></script>

But no dice. So on to the next attempt.

Window -> Preferences -> JavaScript -> Include Path -> User Libraries -> New...

Here I added my jQuery library and referened my file correctly. I can see my jQuery library in JavaScript Resources. It looks just like I'd expect it. But still, my jQuery script is not included in my page.

What am I missing here? Is this such a no-brainer that nobody bothers to properly document how to do this? If it's such a no-brainer, why can't I figure it out? I THOUGHT I had a brain...

Upvotes: 9

Views: 37494

Answers (7)

Anshul Sachdeva
Anshul Sachdeva

Reputation: 21

you can also add dependencies of jquery in your pom.xml and include in the script source: <script type="text/javascript" src="webjars/jquery/2.1.4/jquery.js"></script>

Upvotes: 0

NiLInfinite
NiLInfinite

Reputation: 62

Why are your JSPs in WEB-INF? They are created in 'WebContent' folder. WEB-INF folder should not be included in the class path.

Don't put your work in WEB-INF folder.

Upvotes: 0

user1219387
user1219387

Reputation: 135

You could try using a separate servlet (which I have actually not tried yet) to load up the jQuery.js as static content.

What solved it for me, is putting the jQuery.js file in the /content folder with the rest of html pages, and referencing it relative to root directory for both jsp and html pages.

Upvotes: 0

Samo
Samo

Reputation: 8240

The advice to move my jquery library to such-and-such a location was correct, with a small exception. A new servlet is required to serve up static content files. If anyone else has this problem, something like this should fit the bill:

  <servlet>
    <servlet-name>statCont</servlet-name>
    <servlet-class>
      org.apache.catalina.servlets.DefaultServlet
    </servlet-class>
    <load-on-startup>1</load-on-startup>
  </servlet>

  <servlet-mapping>
    <servlet-name>statCont</servlet-name>
    <url-pattern>*.js</url-pattern>
  </servlet-mapping>

Upvotes: 1

DwB
DwB

Reputation: 38300

Option 1

Include jquery.js in your project

  1. Get jQuery.
  2. Put the jQuery.js file somewhere under the web root, but not under WEB-INF. Perhaps, create a directory named "js" at the same level as the WEB-INF folder in your project (for me, this is under the WebContext folder).
  3. Reference the jquery.js file in your JSP page (example below).

Reference jQuery.js within project

<script type="text/javascript" src="/js/jquery-1.4.3.min.js"></script>
or (to include your context path in the reference if you are using JSTL mapped to prefix "c")
<script type="text/javascript" src="<c:url value="/js/jquery-1.4.3.min.js">"></script>

Option 2

  1. Don't get jquery.js
  2. Load jquery from google CDN. (see below)

Reference jQuery.js from Google CDN

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.3/jquery.min.js" type="text/javascript"></script>

Upvotes: 5

GriffeyDog
GriffeyDog

Reputation: 8376

I think you need to locate the file NOT under the WEB-INF directory, as anything there is not visible to the HTML resulting from your JSP. Try putting your js directory directly under WebContent and changing your reference in the tag accordingly.

EDIT: In response to the comment you left under Jay's answer. How are you referencing the file in your script tag?

You will probably want something like:

<script type="text/javascript" src="/<web-context-root>/js/jquery-1.4.3.min.js"></script> 

where web-context-root is specific to your application and assuming you put your js directory directly under WebContent.

Upvotes: 6

Jay Chandran
Jay Chandran

Reputation: 382

That is correct. You should move the 'js' folder above WEB-INF folder. Then the js file will get picked up by your JSP.

Upvotes: 1

Related Questions