Overview
Other MyFaces Extensions
- ExtVal
- Ext-Script
- [Orchestra]
- [Portlet Bridge]
Community
Development
Sponsorship
Your browser does not support iframes
...
For very simple cases you can inject the ScriptExecutor
directly.
Code Block | |||||||
---|---|---|---|---|---|---|---|
| |||||||
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 | |||||||
---|---|---|---|---|---|---|---|
| |||||||
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 | ||||||
---|---|---|---|---|---|---|
| ||||||
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 | ||||||
---|---|---|---|---|---|---|
| ||||||
<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 | ||||||
---|---|---|---|---|---|---|
| ||||||
<h:outputText value="#{sExec.js['[a:#{bean1.result},b:#{bean2.result}]']['a + b']}"/> |
...