How to include jquery.js in Grails?

后端 未结 10 2060
北海茫月
北海茫月 2021-02-05 06:50

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

相关标签:
10条回答
  • 2021-02-05 07:06

    Update for Grails 2.3 This might help troubleshoot the configuration of jQuery so it can be available from your gsp pages.

    1. First of all if you run the grails install-plugin jquery command it will fail with the 'install-plugin' obsolete message: enter image description here

    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"
    }
    
    1. If you are using Eclipse, do a File search for jquery and see if you already have the files: enter image description here

    2. 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: enter image description here

    3. As mentioned before, the combination of these two lines seems to work (at the top of your gsp pages):

    enter image description here

    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.

    0 讨论(0)
  • 2021-02-05 07:08

    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>
    
    0 讨论(0)
  • 2021-02-05 07:11

    <g:javascript library="jquery/jquery"/>

    0 讨论(0)
  • 2021-02-05 07:13

    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.

    0 讨论(0)
  • 2021-02-05 07:16

    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' />
    
    0 讨论(0)
  • 2021-02-05 07:18

    As per the current docs - http://grails.org/plugin/jquery (Grails version: 1.3 > *) on 18th Sept, 2015


    Installation

    To install the jQuery plugin type this command from your project's root folder:

    grails install-plugin jquery
    

    Targets:

    grails installJQuery
    
    • This target downloads and installs jquery-1.4.2.js and jquery-1.4.2.min.js under web-app/js/jquery/

    The complete jQuery distribution is downloaded and installed under your project's /web-app/js/jQuery folder.


    Usage

    Ajax via jQuery

    To have the Grails' adaptive AJAX support adapt itself to jQuery (rather than the default of Prototype, or another choice like YUI or Dojo):

    Since Grails 1.2:

    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)

    0 讨论(0)
提交回复
热议问题