JSF中的页面加载调用命令按钮

onpage load call commandbutton in JSF

本文关键字:调用 命令 按钮 加载 JSF      更新时间:2023-09-26

我试图在页面加载时将值设置为隐藏变量,并调用将该值发送给managedbean。在页面加载我能够设置值和调用命令按钮,并在托管bean中获得值,但它将无条件循环并执行不止一次。这是我的代码。

<script>
window.onload = setId ;
function setId(){
var facId = document.getElementById('formname:bId').value;
document.getElementById('formname:nameId').value = facId;
document.getElementById('formname:buttonId').click();
}
</script>
<h:inputText id="nameId" value="#{bean.nameId}"></h:inputText>
<h:commandButton id="buttonId" value="fId" action="#{bean.init()}" />

页面加载时连续调用按钮

h:commandButton提交表单并导致整个页面重新加载,因此再次执行脚本并重复此过程。要避免这种情况,请添加

  <h:commandButton id="buttonId" value="fId" >  
      <f:ajax event="click" listener="#{bean.init()}" />  
  </h:commandButton>  

希望能有所帮助