如何在javascript函数中直接更新jsfjava字符串

x759pob2  于 2021-07-09  发布在  Java
关注(0)|答案(3)|浏览(290)

我想知道如何直接从javascript函数更新backingbean中的字符串。
我的示例尝试调用javascript函数(字符串img将在其中更新),然后我希望在save函数中访问它的新更新内容,save函数是按钮的操作事件。
这是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"/>

在backing 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;

}
z18hc3ub

z18hc3ub1#

如果您使用的是primefaces,另一种在javascript和javabean之间传递信息的简单方法是remotecommand函数。

r7xajy2e

r7xajy2e2#

我的代码中的问题是函数名的拼写错误(在javascript中)。所以这个问题中的例子会很好的解决。这是一种可以直接从javascript函数更新javabean变量内容的技术。其中一个非常有用的例子是,当您想要捕获画布的内容并用java将其保存到服务器时。

baubqpgj

baubqpgj3#

javascript函数位于客户端,而托管bean位于服务器端。这就是为什么您需要完整的post或ajax请求。
您可以通过嵌套 <f:setPropertyActionListener><p:commandButton> . 这将用特定值更新托管bean中的特定属性。例如:

<p:commandButton value="Change text" action="#{backbean.save()}">
    <f:setPropertyActionListener value="New text value" target="#{masterpage_bean.img}" />
</p:commandButton>

相关问题