Commandbutton works only on second click until a <

2019-08-15 16:46发布

问题:

I'm working with JSF 2.2.9, i have the following dataTable and buttons:

<h:commandButton id="commandButtonRemoverSelected"
                        actionListener="#{managedBeanName.removeSelected()}"
                        class="btn btn-primary" value="Sim">
                    </h:commandButton>

    <h:dataTable var="bean" value="#{managedBeanName.beans}" styleClass="table table-hover" 
            binding="#{managedBeanName.dataTable}">
                <h:column headerClass="smallColumn">
                    <f:facet name="header">
                        <h:selectBooleanCheckbox valueChangeListener="#{managedBeanName.selectAll}">
                            <f:ajax execute="@form" render="@all" />
                        </h:selectBooleanCheckbox>
                    </f:facet>
                    <h:selectBooleanCheckbox value="#{managedBeanName.registrosSelecionados[bean]}" />
                </h:column>

            </h:dataTable>

The button "commandButtonRemoverSelected" just call actionListener on second click. When i remove the following line from datatabe everything works fine (the commandButton is called on first click):

<h:selectBooleanCheckbox value="#{managedBeanName.registrosSelecionados[bean]}" />

So, my managedBean have a MAP called 'registrosSelecionados' that should store a pair "Bean,Boolean". See:

private Map<Bean, Boolean> registrosSelecionados = new HashMap<Bean, Boolean>();

回答1:

SOLUTION

I solved the problem with a workaround and i would share this:

1 - I noted that every first commandButton click the 'getRegistrosSelecionados()' is called and actionListener don't. So i imagine that JSF is processing getRegistrosSelecionados() and don't processing actionListener, i don't know why and this is the real problem. In second click the actionListener is processed because all checkboxs already sent to ManagedBean, in first click.

2 - So, my workaround is force that each click in checkbox call JSF request and don't wait for commandButton submit. In this way when commandButton is clicked all checkbox was processed.

<h:selectBooleanCheckbox value="#{cc.attrs.managedBeanName.registrosSelecionados[bean]}">
                  <f:ajax execute="@this" render="@this" />
                </h:selectBooleanCheckbox>

My checkbox received the ajax to execute only itself and render itself, when i click in commandButton i just need execute the commandButton and render the form again:

<h:commandButton id="commandButtonRemoverSelected"
                        actionListener="#{cc.attrs.managedBeanName.removeSelected()}"
                        class="btn btn-primary" value="Sim">
                        <f:ajax execute="@this" render="@form" />
                    </h:commandButton>

This workaround works for me, as i didn't found a better solution for this problem.