单击命令按钮时如何在onclick事件触发之前调用托管bean中的方法来设置字符串?

1 jsf primefaces

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

这是主要的xhtml coede剪辑:

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

以下是托管bean的代码:

public String setPrefTmpKey(String tmpKey) { 
  currentTmpKey = tmpKey.trim();    
  currentTmpValue = getChapUserPrefString(currentTmpKey);
  return "selectColorDlgWidget.show();";
}
Run Code Online (Sandbox Code Playgroud)

我究竟做错了什么?

Bal*_*usC 6

你犯了一个概念上的错误.在采取值表达式像属性的任何EL表达式onclick属性由JSF产生HTML输出期间被调用,并且在的特定情况下onclick属性从而不被被点击生成的HTML DOM元素时(而不是,它将执行片的JavaScript的已生成的HTML输出中已存在的代码).如果要在操作事件期间调用辅助bean方法,则应该使用该action属性.它采用方法表达式而不是值表达式.

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

同

public void setPrefTmpKey(String tmpKey) { 
    currentTmpKey = tmpKey.trim();    
    currentTmpValue = getChapUserPrefString(currentTmpKey);
}
Run Code Online (Sandbox Code Playgroud)

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

<p:commandButton value="Edit" id="editColorButton"
    action="#{chapUserPrefMB.setPrefTmpKey('CHAP_ColorOneOrMoreCls')}"
    oncomplete="selectColorDlgWidget.show()" />
Run Code Online (Sandbox Code Playgroud)