Wiki source code of Creating a Groovy Class

Last modified by Simon Urli on 2023/10/10

Hide last authors
Vincent Massol 23.1 1 {{box cssClass="floatinginfobox" title="**Contents**"}}
2 {{toc/}}
3 {{/box}}
4
Manuel Smeria 23.3 5 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>>extensions:Extension.Script Module]].
Jean-Vincent Drean 1.1 6
Vincent Massol 22.1 7 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.
Jean-Vincent Drean 1.1 8
Vincent Massol 22.1 9 = In XWiki Syntax 2.0+ =
Jean-Vincent Drean 1.1 10
Vincent Massol 23.6 11 * Create a page, for example ##Groovy.HelloWorldClass## containing:(((
Manuel Smeria 23.3 12 {{info}}
Simon Urli 25.2 13 This page must have been saved by a user with programming [[rights>>xwiki:Documentation.UserGuide.Features.RightsManagement]] to be executed.
Vincent Massol 23.6 14 {{/info}}
Vincent Massol 22.1 15
16 {{code}}
17 {{groovy}}
18 class HelloWorld
19 {
20 String say()
21 {
22 return "Hello World"
23 }
24 }
25 {{/groovy}}
26 {{/code}}
27 )))
Vincent Massol 24.1 28 * Use this Groovy class from another wiki page, say from ##Main.HelloWorldFromGroovy##:(((
yang ningJian 8.1 29 {{info}}
Simon Urli 25.2 30 This page must have been saved by a user with programming [[rights>>xwiki:Documentation.UserGuide.Features.RightsManagement]] to be executed.
yang ningJian 8.1 31 {{/info}}
Vincent Massol 22.1 32
Silvia Macovei 5.1 33 {{code}}
Vincent Massol 22.1 34 {{include document="Groovy.HelloWorldClass"/}}
35
36 {{groovy}}
37 println new HelloWorld().say()
38 {{/groovy}}
39 {{/code}}
40 )))
41
42 Now when you view ##Main.HelloWorldFromVelocity## you'll see: {{code}}Hello World{{/code}}
43
Manuel Smeria 23.3 44 {{info}}
45 * 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.
Ecaterina Moraru (Valica) 24.3 46 * Several variables such as ##xwiki##, ##doc##, ##xcontext## are already bound to the Groovy execution context and can be used. See the [[Scripting page>>xwiki:Documentation.DevGuide.Scripting]] for more details.
Manuel Smeria 23.3 47 {{/info}}
Vincent Massol 22.1 48
49 = In XWiki Syntax 1.0 =
50
51 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.
52
Vincent Massol 24.1 53 * Create a new page, for example ##Groovy.HelloWorldClass## containing:(((
Manuel Smeria 23.3 54 {{info}}
Simon Urli 25.2 55 * This page must have been saved by a user with programming [[rights>>xwiki:Documentation.UserGuide.Features.RightsManagement]] to be executed.
Manuel Smeria 23.3 56 * When creating a page that has to be accessed via ##parseGroovyFromString## make sure you do not have opening and closing groovy identifiers.
57 {{/info}}
Vincent Massol 22.1 58
59 {{code}}
Silvia Macovei 6.1 60 /* Groovy Class #* */
Jean-Vincent Drean 1.28 61
Vincent Massol 22.1 62 class HelloWorldClass
63 {
64 def xwiki
65 def context
Jean-Vincent Drean 1.1 66
Vincent Massol 22.1 67 void setObjects(xwiki, context)
68 {
69 setXWiki(xwiki)
70 setContext(context)
71 }
Jean-Vincent Drean 1.1 72
Vincent Massol 22.1 73 void setXWiki(xwiki)
74 {
75 this.xwiki = xwiki
Jean-Vincent Drean 1.1 76 }
Jean-Vincent Drean 1.36 77
Vincent Massol 22.1 78 void setContext(context)
79 {
80 this.context = context
Jean-Vincent Drean 1.36 81 }
82
Vincent Massol 22.1 83 String say()
84 {
85 return "Hello World"
Jean-Vincent Drean 1.1 86 }
87 }
Jean-Vincent Drean 1.28 88
Silvia Macovei 6.1 89 /* *# */
Silvia Macovei 5.1 90 {{/code}}
Jean-Vincent Drean 1.1 91
Manuel Smeria 23.3 92 {{info}}
93 * Notice the trick of putting a Velocity comment in the Groovy comment so that the code is not parsed by Velocity.
94 * 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.
95 {{/info}}
Vincent Massol 22.1 96 )))
Vincent Massol 1.48 97
Vincent Massol 22.1 98 == Instantiate and use your class from Velocity ==
Silvia Macovei 6.1 99
Vincent Massol 24.1 100 * Create a new page, for example ##Main.HelloWorldFromVelocity## containing:(((
Silvia Macovei 5.1 101 {{code}}
Silvia Macovei 6.1 102 #set($groovyObject = $xwiki.parseGroovyFromPage("Groovy.HelloWorldClass"))
Jean-Vincent Drean 1.12 103 $groovyObject.setObjects($xwiki, $context)
Vincent Massol 22.1 104 $groovyObject.say()
Silvia Macovei 5.1 105 {{/code}}
Silvia Macovei 6.1 106 )))
Jean-Vincent Drean 1.1 107
Manuel Smeria 23.3 108 * See the result. Feeling groovy ? ;)
109
Vincent Massol 22.1 110 == Instantiate and use your class from Groovy ==
Vincent Massol 7.2 111
Vincent Massol 24.1 112 * Create a new page, for example ##Main.HelloWorldFromGroovy## containing:(((
yang ningJian 8.1 113 {{info}}
Simon Urli 25.2 114 This page must have been saved by a user with programming [[rights>>xwiki:Documentation.UserGuide.Features.RightsManagement]] to be executed.
yang ningJian 8.1 115 {{/info}}
Vincent Massol 22.1 116
yang ningJian 8.1 117 {{code}}
Vincent Massol 22.1 118 <%
RicardoJafe 10.1 119 groovyObject = xwiki.parseGroovyFromPage("Groovy.HelloWorldClass")
Jean-Vincent Drean 1.13 120 groovyObject.setObjects(xwiki, context)
Vincent Massol 22.1 121 print groovyObject.say()
122 %>
yang ningJian 8.1 123 {{/code}}
Silvia Macovei 6.1 124 )))

Get Connected