How to call ad hoc SSJS from a Java Bean

2019-06-05 15:46发布

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?

1条回答
我只想做你的唯一
2楼-- · 2019-06-05 16:08

There are several possibilities for this:

  1. The variable compute is empty
  2. compute contains illegal chars
  3. The code inside compute is malformed / has no correct syntax
  4. 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);
查看更多
登录 后发表回答