Intro

The Intro page provides an overview and describes the motivation for the features described below. This page explains the most important APIs and mechanisms of the scripting module provided by CODI.

ScriptExecutor

For very simple cases you can inject the ScriptExecutor directly.

Using the ScriptExecutor
public class ServerSideScriptingBean
{
    @Inject
    @ScriptLanguage(JavaScript.class)
    private ScriptExecutor scriptExecutor;

    //...

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

The most important part of this example is @ScriptLanguage. As you will see in the other examples, it allows to specify the language in a type-safe way.

ScriptBuilder

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.

Using the ScriptBuilder
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);
    }
}

ScriptEngine

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.

Using the ScriptEngine directly
public class ServerSideScriptingBean
{
    @Inject
    @ScriptLanguage(JavaScript.class)
    private ScriptEngine scriptEngine;

    //...

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

EL Support

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.

Using the scripting support in Value-Expressions
<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:

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

As you have seen on top of this page, the scripting module doesn't have a dependency to JSF. So you need an add-on for this feature. Please contact us, if you are interested in such an add-on. (In the examples provided by CODI you can see a very simple implementation which allows the usage of such a script.)

In some use-cases (e.g. the EL support) we currently just support JavaScript. You will see SPIs,... which will allow the use of other languages for such use-cases as well.
Currently that isn't our highest priority. Please contact us if you are interested in using other languages and you have an use-case which doesn't work with the current implementation.

  • No labels