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

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

我想知道如何直接从javascript函数更新backingbean中的字符串。
我的示例尝试调用javascript函数(字符串img将在其中更新),然后我希望在save函数中访问它的新更新内容,save函数是按钮的操作事件。
这是xhtml文件的摘录:

  1. <Script>
  2. function jsSave(){
  3. var text=document.getElementById("form:img");
  4. text.value="New text value";
  5. }
  6. </Script>
  7. <p:commandButton value="Change text" onclick="jsSave();" action="#{backbean.save()}"/>
  8. <h:outputText value="#{masterpage_bean.img}" id="img"/>

在backing bean中,我有以下代码:

  1. String img;
  2. public String getImg() {
  3. return img;
  4. }
  5. public void setImg(String img) {
  6. this.img = img;
  7. }
  8. public String save(){
  9. String tex;
  10. tex=img;
  11. // I want to do some stuff with the updated value of img here.
  12. return null;
  13. }
z18hc3ub

z18hc3ub1#

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

r7xajy2e

r7xajy2e2#

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

baubqpgj

baubqpgj3#

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

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

相关问题