Creating a Groovy Class
This tutorial illustrates the XWiki.parseGroovyFromPage API method. This method allow you to instantiate a groovy class from both velocity and groovy code.Create a groovy class
- Create a new page, for example Groovy.HelloWorldClass containing :
/* Groovy Class #* */
class groovyClass {
def xwiki;
def context;
void setObjects(xwiki, context) {
setXWiki(xwiki);
setContext(context);
}
void setXWiki(xwiki) {
this.xwiki = xwiki;
}
void setContext(context) {
this.context = context;
}
String helloWorld() {
return "Hello World";
}
}
/* *# */Instantiate and use your class from velocity
- Create a new page, for example "Main.HelloWorldFromVelocity" containing :
#set($groovyObject = $xwiki.parseGroovyFromPage("Groovy.HelloWorldClass"))
$groovyObject.setObjects($xwiki, $context)
$groovyObject.helloWorld()- See the result, feeling groovy ? ;)
Instantiate and use your class from groovy
- Create a new page, for example "Groovy.HelloWorldClass" containing :
<%
groovyObject = xwiki.parseGroovyFromPage("Dev.HelloWorldGroovyClass")
groovyObject.setObjects(xwiki, context)
print(groovyObject.helloWorld())
%>- See the result, feeling groovy ? ;)
Version 2.1 last modified by VincentMassol on 10/12/2007 at 15:56
Document data
Attachments:
No attachments for this document
Comments: 2