Wiki source code of Creating a Groovy Class
Version 23.7 by Vincent Massol on 2015/02/08
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>>platform: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 | ((( | ||
30 | {{info}} | ||
31 | This page must have been saved by a user with programming [[rights>>platform:Features.RightsManagement]] to be executed. | ||
32 | {{/info}} | ||
33 | |||
34 | {{code}} | ||
35 | {{include document="Groovy.HelloWorldClass"/}} | ||
36 | |||
37 | {{groovy}} | ||
38 | println new HelloWorld().say() | ||
39 | {{/groovy}} | ||
40 | {{/code}} | ||
41 | ))) | ||
42 | |||
43 | Now when you view ##Main.HelloWorldFromVelocity## you'll see: {{code}}Hello World{{/code}} | ||
44 | |||
45 | {{info}} | ||
46 | * 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. | ||
47 | * Several variables such as ##xwiki##, ##doc##, ##xcontext## are already bound to the Groovy execution context and can be used. See the [[Scripting page>>DevGuide.Scripting]] for more details. | ||
48 | {{/info}} | ||
49 | |||
50 | = In XWiki Syntax 1.0 = | ||
51 | |||
52 | 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. | ||
53 | |||
54 | * Create a new page, for example ##Groovy.HelloWorldClass## containing: | ||
55 | ((( | ||
56 | {{info}} | ||
57 | * This page must have been saved by a user with programming [[rights>>platform:Features.RightsManagement]] to be executed. | ||
58 | * When creating a page that has to be accessed via ##parseGroovyFromString## make sure you do not have opening and closing groovy identifiers. | ||
59 | {{/info}} | ||
60 | |||
61 | {{code}} | ||
62 | /* Groovy Class #* */ | ||
63 | |||
64 | class HelloWorldClass | ||
65 | { | ||
66 | def xwiki | ||
67 | def context | ||
68 | |||
69 | void setObjects(xwiki, context) | ||
70 | { | ||
71 | setXWiki(xwiki) | ||
72 | setContext(context) | ||
73 | } | ||
74 | |||
75 | void setXWiki(xwiki) | ||
76 | { | ||
77 | this.xwiki = xwiki | ||
78 | } | ||
79 | |||
80 | void setContext(context) | ||
81 | { | ||
82 | this.context = context | ||
83 | } | ||
84 | |||
85 | String say() | ||
86 | { | ||
87 | return "Hello World" | ||
88 | } | ||
89 | } | ||
90 | |||
91 | /* *# */ | ||
92 | {{/code}} | ||
93 | |||
94 | {{info}} | ||
95 | * Notice the trick of putting a Velocity comment in the Groovy comment so that the code is not parsed by Velocity. | ||
96 | * 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. | ||
97 | {{/info}} | ||
98 | ))) | ||
99 | |||
100 | == Instantiate and use your class from Velocity == | ||
101 | |||
102 | * Create a new page, for example ##Main.HelloWorldFromVelocity## containing: | ||
103 | ((( | ||
104 | {{code}} | ||
105 | #set($groovyObject = $xwiki.parseGroovyFromPage("Groovy.HelloWorldClass")) | ||
106 | $groovyObject.setObjects($xwiki, $context) | ||
107 | $groovyObject.say() | ||
108 | {{/code}} | ||
109 | ))) | ||
110 | |||
111 | * See the result. Feeling groovy ? ;) | ||
112 | |||
113 | == Instantiate and use your class from Groovy == | ||
114 | |||
115 | * Create a new page, for example ##Main.HelloWorldFromGroovy## containing: | ||
116 | ((( | ||
117 | {{info}} | ||
118 | This page must have been saved by a user with programming [[rights>>platform:Features.RightsManagement]] to be executed. | ||
119 | {{/info}} | ||
120 | |||
121 | {{code}} | ||
122 | <% | ||
123 | groovyObject = xwiki.parseGroovyFromPage("Groovy.HelloWorldClass") | ||
124 | groovyObject.setObjects(xwiki, context) | ||
125 | print groovyObject.say() | ||
126 | %> | ||
127 | {{/code}} | ||
128 | ))) |