 | Scripting APIs, on top of JSR-223 |
| Bundled With | XWiki Enterprise, XWiki Enterprise Manager |
| Compatibility | XWiki > 2.3 |
View Source
XWiki's scripting features are built on top of JSR-223.
The Script Module offers the following:
- Defines ScriptEvaluatedEvent and ScriptEvaluatingEvent events (see Observation Module) for being called before a script macro executes or just after it's been executed. The ScriptEvaluatedEvent event even allow to cancel the execution of the script macro.
- Provides a ScriptService component role allowing to expose APIs to script macros (see below)
To expose an API to a script macro you simply need to implement the org.xwiki.script.service.ScriptService component role (see Component Module to know more about components).
For example:
@Component
@Named("my")
@Singleton
public class MyScriptService implements ScriptService
{
// Declare all java methods you wish to expose here. For example:
public void doSomething(String whatever)
{
...
}
}
Of course, make sure to register your component in components.txt and to deploy your component JAR in WEB-INF/lib.
Then to use this in a Velocity script macro for example you'll write:
{{velocity}}
$services.my.doSomething("something here")
{{/velocity}}