Wiki source code of Creating a Groovy Class
Version 22.1 by Vincent Massol on 2012/03/29
Show last authors
author | version | line-number | content |
---|---|---|---|
1 | In general, creating a Groovy class in a wiki page is not really recommend and usually it's nicer to create code in Java and make it available to XWiki pages through [[Script Services>>extensions:Extension.Script Module]]. | ||
2 | |||
3 | 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. | ||
4 | |||
5 | = In XWiki Syntax 2.0+ = | ||
6 | |||
7 | * Create a page, for example ##Groovy.HelloWorldClass## containing:((( | ||
8 | {{info}}This page must have been saved by a user with programming [[rights>>platform:Features.RightsManagement]] to be executed{{/info}} | ||
9 | |||
10 | {{code}} | ||
11 | {{groovy}} | ||
12 | class HelloWorld | ||
13 | { | ||
14 | String say() | ||
15 | { | ||
16 | return "Hello World" | ||
17 | } | ||
18 | } | ||
19 | {{/groovy}} | ||
20 | {{/code}} | ||
21 | ))) | ||
22 | * Use this Groovy class from another wiki page, say from ##Main.HelloWorldFromGroovy##:((( | ||
23 | {{info}} | ||
24 | This page must have been saved by a user with programming rights to be executed | ||
25 | {{/info}} | ||
26 | |||
27 | {{code}} | ||
28 | {{include document="Groovy.HelloWorldClass"/}} | ||
29 | |||
30 | {{groovy}} | ||
31 | println new HelloWorld().say() | ||
32 | {{/groovy}} | ||
33 | {{/code}} | ||
34 | ))) | ||
35 | |||
36 | Now when you view ##Main.HelloWorldFromVelocity## you'll see: {{code}}Hello World{{/code}} | ||
37 | |||
38 | {{info}}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.{{/info}} | ||
39 | |||
40 | {{info}}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.{{/info}} | ||
41 | |||
42 | = In XWiki Syntax 1.0 = | ||
43 | |||
44 | 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. | ||
45 | |||
46 | * Create a new page, for example ##Groovy.HelloWorldClass## containing :((( | ||
47 | {{info}}This page must have been saved by a user with programming [[rights>>platform:Features.RightsManagement]] to be executed{{/info}} | ||
48 | |||
49 | {{info}}When creating a page to be accessed via ##parseGroovyFromString##, make sure you do not have opening and closing groovy identifiers.{{/info}} | ||
50 | |||
51 | {{code}} | ||
52 | /* Groovy Class #* */ | ||
53 | |||
54 | class HelloWorldClass | ||
55 | { | ||
56 | def xwiki | ||
57 | def context | ||
58 | |||
59 | void setObjects(xwiki, context) | ||
60 | { | ||
61 | setXWiki(xwiki) | ||
62 | setContext(context) | ||
63 | } | ||
64 | |||
65 | void setXWiki(xwiki) | ||
66 | { | ||
67 | this.xwiki = xwiki | ||
68 | } | ||
69 | |||
70 | void setContext(context) | ||
71 | { | ||
72 | this.context = context | ||
73 | } | ||
74 | |||
75 | String say() | ||
76 | { | ||
77 | return "Hello World" | ||
78 | } | ||
79 | } | ||
80 | |||
81 | /* *# */ | ||
82 | {{/code}} | ||
83 | |||
84 | {{info}}Notice the trick of putting a Velocity comment in the Groovy comment so that the code is not parsed by Velocity.{{/info}} | ||
85 | |||
86 | {{info}}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{{/info}} | ||
87 | ))) | ||
88 | |||
89 | == Instantiate and use your class from Velocity == | ||
90 | |||
91 | * Create a new page, for example ##Main.HelloWorldFromVelocity## containing :((( | ||
92 | {{code}} | ||
93 | #set($groovyObject = $xwiki.parseGroovyFromPage("Groovy.HelloWorldClass")) | ||
94 | $groovyObject.setObjects($xwiki, $context) | ||
95 | $groovyObject.say() | ||
96 | {{/code}} | ||
97 | ))) | ||
98 | * See the result, feeling groovy ? ;) | ||
99 | |||
100 | == Instantiate and use your class from Groovy == | ||
101 | |||
102 | * Create a new page, for example ##Main.HelloWorldFromGroovy## containing :((( | ||
103 | {{info}} | ||
104 | This page must have been saved by a user with programming rights to be executed | ||
105 | {{/info}} | ||
106 | |||
107 | {{code}} | ||
108 | <% | ||
109 | groovyObject = xwiki.parseGroovyFromPage("Groovy.HelloWorldClass") | ||
110 | groovyObject.setObjects(xwiki, context) | ||
111 | print groovyObject.say() | ||
112 | %> | ||
113 | {{/code}} | ||
114 | ))) |