In general, creating a Groovy class in a wiki page is not really recommended 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:
    This 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:
    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

  • 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.
  • 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:
    • This page must have been saved by a user with programming rights to be executed.
    • When creating a page that has 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"
      
    }
    }

    /* *# */
    • Notice the trick of putting a Velocity comment in the Groovy comment so that the code is not parsed by Velocity.
    • 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:
    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()
    %>
Tags:
Created by Jean-Vincent Drean on 2007/02/15 21:50
    

Get Connected