1

单击命令按钮时,如何在 onclick 事件触发之前调用托管 bean 中的方法来设置字符串?无论我尝试什么,都会在表单刷新时调用该方法,而不是在单击按钮时调用该方法。我需要方法中设置的信息在 selectColorDlgWidget.show() 调用的对话框中可用;

这是primefaces xhtml coede snip:

<p:commandButton value="Edit" id="editColorButton" onclick="#{chapUserPrefMB.setPrefTmpKey('CHAP_ColorOneOrMoreCls')}"/>

这是托管bean的代码:

public String setPrefTmpKey(String tmpKey) { 
  currentTmpKey = tmpKey.trim();    
  currentTmpValue = getChapUserPrefString(currentTmpKey);
  return "selectColorDlgWidget.show();";
}

我究竟做错了什么?

4

1 回答 1

6

你犯了一个概念上的错误。onclick在由 JSF 生成 HTML 输出期间调用属性中的任何 EL 表达式,例如属性,在生成 HTML 输出期间调用,onclick因此在单击生成的 HTML DOM 元素时不会调用该属性(相反,它将执行一段 JavaScript生成的 HTML 输出中已经存在的代码)。如果您想在操作事件期间调用支持 bean 方法,那么您应该改用该action属性。它采用方法表达式而不是值表达式。

<p:commandButton value="Edit" id="editColorButton"
    action="#{chapUserPrefMB.setPrefTmpKey('CHAP_ColorOneOrMoreCls')}" />

public void setPrefTmpKey(String tmpKey) { 
    currentTmpKey = tmpKey.trim();    
    currentTmpValue = getChapUserPrefString(currentTmpKey);
}

然后,要在操作完成时打开对话框,只需使用oncomplete属性:

<p:commandButton value="Edit" id="editColorButton"
    action="#{chapUserPrefMB.setPrefTmpKey('CHAP_ColorOneOrMoreCls')}"
    oncomplete="selectColorDlgWidget.show()" />
于 2013-09-14T19:02:57.857 回答