Wiki source code of Creating a Groovy Class
Last modified by Simon Urli on 2023/10/10
Hide last authors
author | version | line-number | content |
---|---|---|---|
![]() |
23.1 | 1 | {{box cssClass="floatinginfobox" title="**Contents**"}} |
2 | {{toc/}} | ||
3 | {{/box}} | ||
4 | |||
![]() |
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]]. |
![]() |
1.1 | 6 | |
![]() |
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. |
![]() |
1.1 | 8 | |
![]() |
22.1 | 9 | = In XWiki Syntax 2.0+ = |
![]() |
1.1 | 10 | |
![]() |
23.6 | 11 | * Create a page, for example ##Groovy.HelloWorldClass## containing:((( |
![]() |
23.3 | 12 | {{info}} |
![]() |
25.2 | 13 | This page must have been saved by a user with programming [[rights>>xwiki:Documentation.UserGuide.Features.RightsManagement]] to be executed. |
![]() |
23.6 | 14 | {{/info}} |
![]() |
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 | ))) | ||
![]() |
24.1 | 28 | * Use this Groovy class from another wiki page, say from ##Main.HelloWorldFromGroovy##:((( |
![]() |
8.1 | 29 | {{info}} |
![]() |
25.2 | 30 | This page must have been saved by a user with programming [[rights>>xwiki:Documentation.UserGuide.Features.RightsManagement]] to be executed. |
![]() |
8.1 | 31 | {{/info}} |
![]() |
22.1 | 32 | |
![]() |
5.1 | 33 | {{code}} |
![]() |
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 | |||
![]() |
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. | ||
![]() |
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. |
![]() |
23.3 | 47 | {{/info}} |
![]() |
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 | |||
![]() |
24.1 | 53 | * Create a new page, for example ##Groovy.HelloWorldClass## containing:((( |
![]() |
23.3 | 54 | {{info}} |
![]() |
25.2 | 55 | * This page must have been saved by a user with programming [[rights>>xwiki:Documentation.UserGuide.Features.RightsManagement]] to be executed. |
![]() |
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}} | ||
![]() |
22.1 | 58 | |
59 | {{code}} | ||
![]() |
6.1 | 60 | /* Groovy Class #* */ |
![]() |
1.28 | 61 | |
![]() |
22.1 | 62 | class HelloWorldClass |
63 | { | ||
64 | def xwiki | ||
65 | def context | ||
![]() |
1.1 | 66 | |
![]() |
22.1 | 67 | void setObjects(xwiki, context) |
68 | { | ||
69 | setXWiki(xwiki) | ||
70 | setContext(context) | ||
71 | } | ||
![]() |
1.1 | 72 | |
![]() |
22.1 | 73 | void setXWiki(xwiki) |
74 | { | ||
75 | this.xwiki = xwiki | ||
![]() |
1.1 | 76 | } |
![]() |
1.36 | 77 | |
![]() |
22.1 | 78 | void setContext(context) |
79 | { | ||
80 | this.context = context | ||
![]() |
1.36 | 81 | } |
82 | |||
![]() |
22.1 | 83 | String say() |
84 | { | ||
85 | return "Hello World" | ||
![]() |
1.1 | 86 | } |
87 | } | ||
![]() |
1.28 | 88 | |
![]() |
6.1 | 89 | /* *# */ |
![]() |
5.1 | 90 | {{/code}} |
![]() |
1.1 | 91 | |
![]() |
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}} | ||
![]() |
22.1 | 96 | ))) |
![]() |
1.48 | 97 | |
![]() |
22.1 | 98 | == Instantiate and use your class from Velocity == |
![]() |
6.1 | 99 | |
![]() |
24.1 | 100 | * Create a new page, for example ##Main.HelloWorldFromVelocity## containing:((( |
![]() |
5.1 | 101 | {{code}} |
![]() |
6.1 | 102 | #set($groovyObject = $xwiki.parseGroovyFromPage("Groovy.HelloWorldClass")) |
![]() |
1.12 | 103 | $groovyObject.setObjects($xwiki, $context) |
![]() |
22.1 | 104 | $groovyObject.say() |
![]() |
5.1 | 105 | {{/code}} |
![]() |
6.1 | 106 | ))) |
![]() |
1.1 | 107 | |
![]() |
23.3 | 108 | * See the result. Feeling groovy ? ;) |
109 | |||
![]() |
22.1 | 110 | == Instantiate and use your class from Groovy == |
![]() |
7.2 | 111 | |
![]() |
24.1 | 112 | * Create a new page, for example ##Main.HelloWorldFromGroovy## containing:((( |
![]() |
8.1 | 113 | {{info}} |
![]() |
25.2 | 114 | This page must have been saved by a user with programming [[rights>>xwiki:Documentation.UserGuide.Features.RightsManagement]] to be executed. |
![]() |
8.1 | 115 | {{/info}} |
![]() |
22.1 | 116 | |
![]() |
8.1 | 117 | {{code}} |
![]() |
22.1 | 118 | <% |
![]() |
10.1 | 119 | groovyObject = xwiki.parseGroovyFromPage("Groovy.HelloWorldClass") |
![]() |
1.13 | 120 | groovyObject.setObjects(xwiki, context) |
![]() |
22.1 | 121 | print groovyObject.say() |
122 | %> | ||
![]() |
8.1 | 123 | {{/code}} |
![]() |
6.1 | 124 | ))) |