|Scripting APIs, on top of JSR-223|
|License||GNU Lesser General Public License 2.1|
XWiki Enterprise, XWiki Enterprise Manager
XWiki > 2.3
Table of contents
XWiki's scripting features are built on top of.
The Script Module offers the following:
- Defines ScriptEvaluatedEvent and ScriptEvaluatingEvent events (see ) 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)
- Allow setting and accessing script binding trough ScriptContext
Setting script binding
private ScriptContextManager manager;
public void setMyVarForNextScript()
// Best practice since 8.3 to access the current ScriptContext
ScriptContext scriptContext = this.manager.getCurrentScriptContext();
// #getCurrentScriptContext does not exist before 8.3
// ScriptContext scriptContext = this.manager.getScriptContext();
scriptContext.setAttribute("myvar", "my value", ScriptContext.ENGINE_SCOPE);
Any script executed after this will be able to use myvar variable directly. This is true also for Velocity since 8.3.
To expose an API to a script macro you simply need to implement the org.xwiki.script.service.ScriptService component role (see to know more about components).
public class MyScriptService implements ScriptService
// Declare all java methods you wish to expose here. For example:
public void doSomething(String whatever)
Then to use this in a Velocity script macro for example you'll write:
Since the Velocity try() directive has been introduced the new best practice is:
- Script Services should throw exceptions (instead of try/catching them and returning null and setting an xcontext property as before)
- If Velocity scripts (or any script) wish to handle the exceptions, they should use the contentvars.vm for example) will catch it and display it to the user. . If they don’t want to, they don’t have to do anything since the Macro Transformation or the template (
- Existing Script APIs in Java should not be modified as that would break backward compatibility. New signatures can be added and old one deprecated and moved to the legacy modules. After new signatures have been introduced, existing Velocity scripts can be updated to use the new signatures and to use the #try directive if needed.