我想提交一个按钮点击的数据表,但该操作在第一次单击时不会被调用.这是我的代码:
<h:panelGrid id="addToThisDepartmentPanel"> <h:outputText value="#{messageDataBean.message}" rendered="#{messageDataBean.isSuccess eq false}" style="color:red" id="addToThisDepartmentMessage"/> <h:form> <h:dataTable value="#{systemResultViewUtil.systemUserDetailDataBeansList}" var="departmentdetail" id="addToThisDepartmentDataTable" rendered="#{systemResultViewUtil.systemUserDetailDataBeansList.size() gt 0}"> <h:column> <f:facet name="header"> Name </f:facet> <h:outputText value="#{departmentdetail.name}" style="text-align: left;padding-right: 120px" /> </h:column> <h:column> <f:facet name="header"> Current Department </f:facet> <h:outputText value="#{departmentdetail.depName}" style="text-align: left;padding-right: 120px" /> </h:column> <h:column> <f:facet name="header"> Add To This </f:facet> <h:selectBooleanCheckBox value="#{departmentdetail.cheked}" style="text-align: left;padding-right: 120px"/> </h:column> </h:dataTable> <a4j:commandButton oncomplete="if(#{messageDataBean.isSuccess eq true}){ closeAddToThisDepartmentDialog()}" value="Add TO This Department" action="#{departmentServiceBean.addEmployeeToThisDepartment}" id="addToThisDepartmentButton" render=":addToThisDepartmentMessage :message" execute=":addToThisDepartmentDataTable" /> </h:form> </h:panelGrid>
这个问题被称为
JSF spec issue 790.如果ajax渲染一些内容,而这些内容又包含< h:form>,那么它的视图状态将会丢失,这导致该表单中的第一个动作不会调用任何东西.在第一个动作的ajax响应中,表单将获得新的视图状态,因此任何后续操作都将成功.
原文链接:https://www.f2er.com/ajax/160068.html这是为即将到来的JSF 2.2修正的.但是现在用JSF 2.0 / 2.1,你必须引入一个解决方法.你首先需要给出< h:form>一个固定的ID是yourFormId在下面的例子中:
<h:panelGrid id="addToThisDepartmentPanel"> ... <h:form id="yourFormId"> ...
那么你还需要在render属性中引用它:
<f:ajax ... render=":addToThisDepartmentPanel :yourFormId" />
同样的故事也适用于< a4j:xxx>组件.
<a4j:commandButton ... render=":addToThisDepartmentPanel :yourFormId" />
也可以看看:
> h:commandButton/h:commandLink does not work on first click,works only on second click
> Communication in JSF 2.0 – Ajax rendering of content which contains another form