问题描述
我正在修改当前执行的现有代码
I am modifiyng exisiting code which currently does
currentDocument.setValue("field", requestScope.variable);
这很好用,但我现在需要让它更动态一些,所以我将 requestScope.variable
存储为字符串.我知道我可以做到:
This works well, but I now need to make it a little more dynamic so, I have the requestScope.variable
stored as a string. I know I can do:
requestScope["variable"]
但这取决于我知道它在哪个范围内.鉴于字符串可能是 requestScope.variable
甚至 applicationScope.variable
我认为这条路线是关闭的我.
but this relies on me knowing which scope it is in. Given that the string could be requestScope.variable
or even applicationScope.variable
I think this route is closed to me.
我还想过根据 .但认为必须有一个更直接的选择.
I had also thought of splitting the string based on the . but think that there must be a more straightforward option.
我该怎么做?
推荐答案
使用 ${javascript:'requestScope.variable'}
获取定义为字符串的范围变量的值.
Use ${javascript:'requestScope.variable'}
to get the value of the scope variable which is defined as a string.
你的例子是
currentDocument.setValue("field", ${javascript:'requestScope.variable'});
${javascript:...}
在这里用作预处理器并将结果插入到代码中.
${javascript:...}
works here a preprocessor and inserts the result into the code.
这是一个完整的例子:
<?xml version="1.0" encoding="UTF-8"?>
<xp:view
xmlns:xp="http://www.ibm.com/xsp/core">
<xp:this.beforePageLoad><![CDATA[#{javascript:
sessionScope.test = "this is the value of sessionScope.test";
viewScope.other = "this is the value of viewScope.other";
requestScope.myScopeVariable = "viewScope.other";
}]]></xp:this.beforePageLoad>
<xp:text
escape="true"
id="computedField1"
value="#{javascript:
var valueFromSessionScope = ${javascript: 'sessionScope.test'};
var valueFromViewScope = ${javascript: requestScope.myScopeVariable};
valueFromSessionScope + ' ... ' + valueFromViewScope;
}">
</xp:text>
</xp:view>
结果显示:这是 sessionScope.test 的值 ... 这是 viewScope.other 的值
这篇关于基于字符串检索 Xpages 范围变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!