I need to call ssjs from a java bean similar to this this issue. The issue is that the code I need to execute comes from a configuration document, and may look like:
getComponent("xxx").getValue();
I have built a version that does :
String compute = doc.getItemValueString("SSJSStuff");
String valueExpr = "#{javascript:" + compute + "}";
FacesContext fc = FacesContext.getCurrentInstance();
Application app = fc.getApplication();
ValueBinding vb = app.createValueBinding(valueExpr);
String vreslt = vb.getValue(fc).toString();
but I get "Exception in xxx: com.ibm.xsp.exception.EvaluationExceptionEx: Error while executing JavaScript computed expression"
I think I am close, but I do not see over the hill.. Any thoughts?
There are several possibilities for this:
- The variable compute is empty
- compute contains illegal chars
- The code inside compute is malformed / has no correct syntax
No object is returned in your SSJS code:
If your SSJS code does not return something, vb.getValue(fc) returns null. A toString() will fail. To prevent this, you should cast your returning object explicitly:
vreslt = (String) vb.getValue(fc);
Hope this helps
Sven
EDIT:
After re-reading your post, I saw that you want to do a getComponent in your dynamic SSJS Code. This won't work with a value binding added to the javax.faces.application.Application. For this, you have to use the com.ibm.xsp.page.compiled.ExpressionEvaluatorImpl object instead:
String valueExpr = "#{javascript:" + compute + "}";
FacesContext fc = FacesContext.getCurrentInstance();
ExpressionEvaluatorImpl evaluator = new ExpressionEvaluatorImpl( fc );
ValueBinding vb = evaluator.createValueBinding( fc.getViewRoot(), valueExpr, null, null);
vreslt = (String) vb.getValue(fc);