这有点棘手,但仍然可行。
让我们从你的bean开始:
@ManagedBean
@ViewScoped
public class ViewingSessionBean implements Serializable {
private String variable;
public String getVariable() {
return this.variable;
}
public void updateVariableValue(Document document) {
variable = restClient.call();
}
}
<h:form id="form">
<p:commandLink id="firstLink"
actionListener="#{viewSessionBean.updateVariableValue(document)}"
update=":form:secondLink"
oncomplete="$('#form\\:secondLink').click()"/>
<p:commandLink id="secondLink" onclick="alert('#{viewSessionBean.variable}')" style="display: none;"/>
</h:form>
第一:它使用了两个commandLinks,而不仅仅是一个,为什么?因为在第一个链接的oncomplete调用时,bean变量已经是最新的,但您的html代码不是最新的。因此,为了获得bean变量的更新值,我们做了以下事情:
-
-
在第二个链接上进行ajax更新,以从bean中获取更新后的值;
-
调用oncomplete方法并单击第二个链接(现在用正确的值更新);
第三:第二个链接设置为显示:无样式,使其在屏幕中不可见。
现在只想一想:JSF与JavaScript配合使用效果很好,但有时我们不得不使用一些像这样的笨拙技巧来完成“简单”的任务。我并不是说JSF不好,但我认为我们可以用一种更“开箱即用”的方法来处理这类事情。不过这只是我的意见。