Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to include jquery.js in Grails?

Tags:

I have a Grails 2.0.0 project that was created using grails create-app. In my HTML and GSP files, I'm trying to include jquery.js. I've tried all of the following without success:

<script type="text/javascript" src="jquery/jquery-1.7.1.js"></script> <script type="text/javascript" src="jquery/jquery.js"></script> <g:javascript library="jquery"/> 

The first two <script> tags results in 404 Not Found (verified with Firebug). The <g:javascript>tag results in nothing being included (verified using view source).

On my Grails application's home page, it indicates that jquery 1.7.1 is installed (under "INSTALLED PLUGINS").

What is the correct way in Grails to include the jquery .js file?

Follow-up: The .GSP file:

<%@ page contentType="text/html;charset=UTF-8" %> <html>   <head>     <title>Test</title>     <g:javascript library="jquery/jquery"/>   </head>   <body>     <h1>Test</h1>   </body> </html> 

Results in the follow HTML source:

<html>   <head>     <title>Test</title>    </head>   <body>     <h1>Test</h1>   </body> </html> 

Note the lack of jquery.js being included.

Follow-up 2:

I'm creating my app using grails create-app:

13:56:40 ~/grailsDev $ grails create-app helloworld | Created Grails Application at /Users/steve/grailsDev/helloworld 13:56:57 ~/grailsDev $ cd helloworld/ 13:57:06 ~/grailsDev/helloworld $ ls -al web-app/js total 8 drwxr-xr-x  3 steve  staff  102 Jan 21 13:56 . drwxr-xr-x  7 steve  staff  238 Dec 15 08:04 .. -rw-r--r--  1 steve  staff  183 Dec 14 22:56 application.js 13:57:23 ~/grailsDev/helloworld $ grails -version  Grails version: 2.0.0 
like image 411
Steve Kuo Avatar asked Jan 21 '12 21:01

Steve Kuo


2 Answers

Apparently <r:layoutResources/> needs to be included in <head> (after <q:javascript library='jquery' />). The following actually works:

<%@ page contentType="text/html;charset=UTF-8" %> <html>   <head>     <title>Simple GSP page</title>     <g:javascript library='jquery' />     <r:layoutResources/>   </head>   <body>     Place your content here   </body> </html> 
like image 125
Steve Kuo Avatar answered Nov 07 '22 05:11

Steve Kuo


The jquery plugin is installed by default in 2.0 - see grails-app/conf/BuildConfig.groovy. To use jquery.js in a GSP just add this line:

<g:javascript library='jquery' /> 
like image 31
Burt Beckwith Avatar answered Nov 07 '22 05:11

Burt Beckwith