Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Comment: Migrated to Confluence 4.0

...

For very simple cases you can inject the ScriptExecutor directly.

Code Block
javajava
titleUsing the ScriptExecutor
java
public class ServerSideScriptingBean
{
    @Inject
    @ScriptLanguage(JavaScript.class)
    private ScriptExecutor scriptExecutor;

    //...

    private Double calc()
    {
       return this.scriptExecutor.eval("10 + 4", Double.class);
    }
}

...

Usually you will have to parameterize the script. You could also use the ScriptExecutor for it, however, that would be a bit too verbose. In such a case it's easier to inject the ScriptBuilder.

java
Code Block
java
titleUsing the ScriptBuilder
java
public class ServerSideScriptingBean
{
    @Inject
    @ScriptLanguage(JavaScript.class)
    private ScriptBuilder scriptBuilder;

    //...

    private Double calc(Double a, Double b)
    {
       return this.scriptBuilder
                .script("x + y")
                .namedArgument("x", a)
                .namedArgument("y", b)
                .eval(Double.class);
    }
}

...

If you have to use some features of the ScriptEngine which aren't supported by the mechanisms above, please contact the community. Furthermore, you can directly inject and use the ScriptEngine.

java
Code Block
java
titleUsing the ScriptEngine directly
java
public class ServerSideScriptingBean
{
    @Inject
    @ScriptLanguage(JavaScript.class)
    private ScriptEngine scriptEngine;

    //...

    private Double calc() throws ScriptException 
    {
        return (Double)this.scriptEngine.eval("3 + 4");
    }
}

...

If you would like to use scripts in your JSF pages but you don't like to expose them to the client, you can eval them during the rendering process. For sure it's required that the logic of the scripts allow to be executed during the rendering process.

Code Block
xhtmlxhtml
titleUsing the scripting support in Value-Expressions
xhtml
<h:outputText value="#{sExec.js['[a:3,b:4]']['a + b']}"/>

Usually you will need the values of other value expressions as arguments for your script.
In some examples you will see something like the following example:

Code Block
xhtmlxhtml
titleUsing values of Value-Bindings as arguments
xhtml
<h:outputText value="#{sExec.js['[a:#{bean1.result},b:#{bean2.result}]']['a + b']}"/>

...