我想知道如何直接从 Javascript 函数更新支持 bean 中的字符串。
我的示例尝试调用 Javascript 函数(其中字符串 img 将更新),然后我想在保存函数(即按钮的操作事件)中访问其新更新的内容。
这是 Xhtml 文件的摘录:
<Script>
function jsSave(){
var text=document.getElementById("form:img");
text.value="New text value";
}
</Script>
<p:commandButton value="Change text" onclick="jsSave();" action="#{backbean.save()}"/>
<h:outputText value="#{masterpage_bean.img}" id="img"/>
在支持 bean 中,我有以下代码:
String img;
public String getImg() {
return img;
}
public void setImg(String img) {
this.img = img;
}
public String save(){
String tex;
tex=img;
// I want to do some stuff with the updated value of img here.
return null;
}
最佳答案
JavaScript 函数位于客户端,而托管 bean 位于服务器端。这就是为什么您需要完整的 POST 或 AJAX 请求。
您可以通过嵌套<f:setPropertyActionListener>
来使用内置的JSF功能。到<p:commandButton>
。这将使用特定值更新托管 bean 中的特定属性。例如:
<p:commandButton value="Change text" action="#{backbean.save()}">
<f:setPropertyActionListener value="New text value" target="#{masterpage_bean.img}" />
</p:commandButton>
关于java - 如何直接在 JavaScript 函数中更新 JSF java 字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22329884/