我正在尝试在页面加载时将值设置为隐藏变量并调用以将该值发送到managedbean.Onpage加载我能够设置值并调用commandbutton并在托管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()}" />
在页面加载时,它会连续调用该按钮。
答案 0 :(得分:0)
h:commandButton
提交表单并导致整个页面重新加载,因为您的脚本再次执行并重复此操作。为了避免这个添加
<h:commandButton id="buttonId" value="fId" >
<f:ajax event="click" listener="#{bean.init()}" />
</h:commandButton>
希望这有帮助