可能重复:
Displaying a message from managed bean with primefaces confirmation dialog component
设置:jsf 2和primefaces。
我的xhtml页面包含一个带有图形的画布。用户可以单击图形的某个区域,这会调用一个js函数,该函数返回特定于该区域的字符串,我们称之为“textFromClickedRegion”。该字符串存储在inputHidden字段中。
javascript中的下一行将包含inputhidden字段的表单提交给bean,并打开一个confirmDialog(在primefaces中),其标题应该由bean中的字符串自定义。
问题:对话框打开,但字符串永远不会获得bean的值(这只是textFromClickedRegion)。
js脚本:
textFromClickedRegion = region.getText();
document.getElementById('formId:nameClicked').value = textFromClickedRegion;
document.getElementById('formId:submitNameClicked').click();
xhtml:
<h:form id="formId">
<h:inputHidden id="nameClicked" value="#{reportBean.nameClicked}"/>
<p:commandButton id="submitNameClicked" type="button" process="@form" onclick="cd.show();"/>
</h:form>
<p:confirmDialog id ="dialogBox" message="#{reportBean.nameClicked}, are you sure about destroying the world?"
header="Initiating destroy process" severity="alert"
widgetVar="cd">
<p:commandButton value="Yes Sure" oncomplete="confirmation.hide();"/>
<p:commandButton value="Not Yet" onclick="confirmation.hide();" type="button" />
</p:confirmDialog>
如何让confirmDialog显示js函数返回的字符串?
[注意:你会认识到以上是Primefaces手册和post by BalusC在客户端和服务器以及jsf / js之间通信的混合 - 感谢他。“
答案 0 :(得分:0)
这是一个有效的例子:
import javax.annotation.PostConstruct;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
@ManagedBean
@ViewScoped
public class Bean {
private String value;
@PostConstruct
public void postConstruct() {
value = "SERVER SIDE VALUE";
}
public String getValue() {
return value;
}
public void setValue(String value) {
this.value = value;
}
}
<h:head>
<script>
function updateElementValue(id,value){
document.getElementById(id).value = value;
}
</script>
</h:head>
<h:body>
<h:form id="form" prependId="false">
<p:commandButton value="UPDATE CLIENT SIDE VALUE OF INPUT-HIDDEN"
onclick="updateElementValue('nameInputHiddenId',
'CLIENT SIDE VALUE');
return false;"/>
<p:commandButton value="UPDATE SERVER SIDE VALUE OF INPUT-HIDDEN"
process="@form"
update="dialogId"
oncomplete="dialogWidgetVar.show();" />
<h:inputHidden id="nameInputHiddenId" value="#{bean.value}" />
<p:dialog id="dialogId" widgetVar="dialogWidgetVar">
<h:outputText id="nameOutputTextId" value="#{bean.value}" />
<p:commandButton value="Yes" onclick="dialogWidgetVar.hide();" />
<p:commandButton value="No" onclick="dialogWidgetVar.hide();"/>
</p:dialog>
</h:form>
</h:body>