Versions Compared

Key

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

...

For very simple cases you can inject the ScriptExecutor directly.

Code Block
java
java
titleUsing the ScriptExecutorjava
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.

Code Block
java
java
titleUsing the ScriptBuilderjava
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.

Code Block
java
java
titleUsing the ScriptEngine directlyjava
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
xhtml
xhtml
titleUsing the scripting support in Value-Expressionsxhtml
<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
xhtml
xhtml
titleUsing values of Value-Bindings as argumentsxhtml
<h:outputText value="#{sExec.js['[a:#{bean1.result},b:#{bean2.result}]']['a + b']}"/>

...