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