本文介绍了如何从 Java Bean 调用临时 SSJS的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要从类似于此此问题的 java bean 调用 ssjs.问题是我需要执行的代码来自一个配置文件,可能看起来像:

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();

但我得到xxx中的异常:com.ibm.xsp.exception.EvaluationExceptionEx:执行JavaScript计算表达式时出错"

我想我已经很近了,但我没有看到山上的东西..有什么想法吗?

I think I am close, but I do not see over the hill.. Any thoughts?

推荐答案

有几种可能:

  1. 变量compute为空

计算包含非法字符

compute 中的代码格式错误/语法不正确

The code inside compute is malformed / has no correct syntax

您的 SSJS 代码中没有返回任何对象:

No object is returned in your SSJS code:

如果您的 SSJS 代码没有返回任何内容,vb.getValue(fc) 将返回 null.toString() 会失败.为了防止这种情况,您应该显式转换返回的对象:

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);

希望能帮到你

斯文

编辑:
重新阅读您的帖子后,我看到您想在动态 SSJS 代码中执行 getComponent.这不适用于添加到 javax.faces.application.Application 的值绑定.为此,您必须改用 com.ibm.xsp.page.compiled.ExpressionEvaluatorImpl 对象:

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);

这篇关于如何从 Java Bean 调用临时 SSJS的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-20 22:27