Creating a Groovy Class

Version 22.1 by Vincent Massol on 2012/03/29

In general, creating a Groovy class in a wiki page is not really recommend and usually it's nicer to create code in Java and make it available to XWiki pages through Script Services.

However there might be a few cases when it's handy to create a Groovy class in a wiki page and reuse it from several other wiki pages.

In XWiki Syntax 2.0+

  • Create a page, for example Groovy.HelloWorldClass containing:

    InformationThis page must have been saved by a user with programming rights to be executed 

    {{groovy}}
    class HelloWorld
    {
     String say()
      {
       return "Hello World"
      }
    }
    {{/groovy}}
  • Use this Groovy class from another wiki page, say from Main.HelloWorldFromGroovy:
    Information

     This page must have been saved by a user with programming rights to be executed 

    {{include document="Groovy.HelloWorldClass"/}}

    {{groovy}}
    println new HelloWorld().say()
    {{/groovy}}

Now when you view Main.HelloWorldFromVelocity you'll see: Hello World

Information

Note that with this strategy it's not possible to call the HelloWorld class from Velocity and you'll need to use a Groovy macro to see it.

Information

Several variables such as xwiki, doc, xcontext are already bound to the Groovy execution context and can be used. See the Scripting page for more details.

In XWiki Syntax 1.0

You'll need to use the XWiki.parseGroovyFromPage API method. This method allow you to instantiate a Groovy class from both Velocity and Groovy scripts.

  • Create a new page, for example Groovy.HelloWorldClass containing :

    InformationThis page must have been saved by a user with programming rights to be executed 

    Information

    When creating a page to be accessed via parseGroovyFromString, make sure you do not have opening and closing groovy identifiers.

    /* Groovy Class #* */

    class HelloWorldClass
    {
     def xwiki
     def context

     void setObjects(xwiki, context)
      {
       setXWiki(xwiki)
       setContext(context)
      }

     void setXWiki(xwiki)
      {
       this.xwiki = xwiki
      }

     void setContext(context)
      {
       this.context = context
      }

     String say()
      {
       return "Hello World"
      }
    }

    /* *# */
    Information

    Notice the trick of putting a Velocity comment in the Groovy comment so that the code is not parsed by Velocity.

    Information

    As you can see, we can get and store the XWiki and Context objects in the class to be able to use them; Their use is not illustrated in this tutorial though

Instantiate and use your class from Velocity

  • Create a new page, for example Main.HelloWorldFromVelocity containing :
    #set($groovyObject = $xwiki.parseGroovyFromPage("Groovy.HelloWorldClass"))
    $groovyObject.setObjects($xwiki, $context)
    $groovyObject.say()
  • See the result, feeling groovy ? emoticon_wink

Instantiate and use your class from Groovy

  • Create a new page, for example Main.HelloWorldFromGroovy containing :
    Information

    This page must have been saved by a user with programming rights to be executed 

    <%
    groovyObject = xwiki.parseGroovyFromPage("Groovy.HelloWorldClass")
    groovyObject.setObjects(xwiki, context)
    print groovyObject.say()
    %>

Get Connected