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
Update for Grails 2.3 This might help troubleshoot the configuration of jQuery so it can be available from your gsp pages.
install-plugin jquery
command it will fail with the 'install-plugin' obsolete message:
So most likely you already have it configured in your BuildConfig.groovy like this (note is runtime, not compile):
plugins {
// ... some other plugins here ...
runtime ":jquery:1:11:1"
}
If you are using Eclipse, do a File search for jquery and see if you already have the files:
Confirm that your js
directory is not empty, it is located inside the web-app
directory. If js
has no files or directories then just copy them from what you got in in step 2:
As mentioned before, the combination of these two lines seems to work (at the top of your gsp pages):
If jQuery is not the very first javascript library to load you might have problems. Check your layouts/main.gsp
file if you have one. You might need to add jquery to all your pages just so it is at the very top of your html source.
Hope that helps.
Note: At the time of this posting (April 2015) Grails 3.0 has been released and it appears to be incompatible with Grails 2.X projects in the way they are configured. Hopefully it will be better documented to avoid the issues with 2.X.
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>
<g:javascript library="jquery/jquery"/>
In grails 2.x you can also do:
grails.resources.modules = {
core {
dependsOn 'jquery, jquery-ui'
}
}
in Config.groovy
Then in your GSP simply place the following in your HEAD
element:
<r:require module="core"/>
The advantage is you can specify other CSS/JS files to depend on, makes it nice and clean in the GSP. This is also where you can override the versions of jQuery/jQuery-UI.
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' />
As per the current docs - http://grails.org/plugin/jquery (Grails version: 1.3 > *) on 18th Sept, 2015
To install the jQuery plugin type this command from your project's root folder:
grails install-plugin jquery
grails installJQuery
The complete jQuery distribution is downloaded and installed under your project's /web-app/js/jQuery folder.
To have the Grails' adaptive AJAX support adapt itself to jQuery (rather than the default of Prototype, or another choice like YUI or Dojo):
Add this line to your layout-file
<g:javascript library="jquery" plugin="jquery"/>
and the following to your grails-app/conf/config.groovy
grails.views.javascript.library="jquery"
alternatively you can use:
<g:javascript library="jquery" />
(without plugin="jquery") but you will need to call the grails installJQuery target (see Installation Tab)