Action <p:commandButton does not call Method in Managedbean

Asked

Viewed 581 times

2

I am trying to call a method in my Managedbean, however the form is not submitted. Detail. I have a dialog that inside it I have some includes of other xhtml files, follows as this up to the moment

This is my dialog

<h:form id="modalMecanica">
                            <p:dialog style="text-align: center" header="Cadastrar Mecânica"
                                widgetVar="modalMecanica" resizable="false" modal="true"
                                width="1050" height="630">

                                <p:outputPanel>
                                    <ui:include src="bancoPerguntasQuestaoQuiz.xhtml" />
                                </p:outputPanel>

                                <p:outputPanel
                                    rendered="#{bancoPerguntasMBean.mecanicaSelecionada == '2'}">
                                    <ui:include src="bancoPerguntasQuestaoVerdadeiroFalso.xhtml" />
                                </p:outputPanel>

                                <p:outputPanel
                                    rendered="#{bancoPerguntasMBean.mecanicaSelecionada == '3'}">
                                    <ui:include src="bancoPerguntasQuestaoDescritiva.xhtml" />
                                </p:outputPanel>

                                <p:outputPanel
                                    rendered="#{bancoPerguntasMBean.mecanicaSelecionada == '4'}">
                                    <ui:include src="bancoPerguntasQuestaoAssociacao.xhtml" />
                                </p:outputPanel>

                                <p:outputPanel
                                    rendered="#{bancoPerguntasMBean.mecanicaSelecionada == '5'}">
                                    <ui:include src="bancoPerguntasQuestaoArrastaSolta.xhtml" />
                                </p:outputPanel>

                            </p:dialog>
                        </h:form>

And this is my file I’m trying to call the method unsuccessfully in MB

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

Alternative

                    <p:selectBooleanCheckbox
                        value="#{bancoPerguntasMBean.novaAlternativaCorretaQuiz}"
                        itemLabel="Verdadeira" />
                    <br />
                    <p:inputTextarea rows="3" cols="100" maxlength="1000"
                        value="#{bancoPerguntasMBean.novaAlternativaQuiz}"
                        placeholder="Digite uma alternativa"
                        counter="contadorCaracteresAlt"
                        counterTemplate="{0} caracteres restantes."
                        styleClass="inputAlternativaQuiz input-padrao">
                        <p:ajax event="keypress"
                            oncomplete="keyPressInputObrigatorio('inputAlternativaQuiz');" />
                    </p:inputTextarea>
                    <p:spacer width="20px" />

                    <br />
                    <h:outputText id="contadorCaracteresAlt" />
                    <br />
                    <p:commandButton value="Criar Alternativa"
                        action="#{bancoPerguntasMBean.criarNovaAlternativaQuiz}">
                        <f:ajax render="quizAlternativasPanel" execute="@this"
                            resetValues="true" />
                    </p:commandButton>


                </p:panel></td>
        </tr>

        <tr>
            <th>Alternativas Criadas</th>
        </tr>
        <tr>
            <td><p:panel id="quizAlternativasListPanel"
                    styleClass="painelLimpo">
                    <p:dataTable var="alternativa" id="alternativasTable"
                        value="#{bancoPerguntasMBean.listaQuizAlternativas}"
                        emptyMessage="Nenhuma alternativa foi criada ainda."
                        tableStyleClass="table-list-contents">
                        <p:column style="background-color:#fff;">
                            <p:commandButton
                                value="(x) #{alternativa.alternativa} #{alternativa.correta == 1 ? ' - Correta' : ' - Errada'}"
                                style="background-color:#FF4500;"
                                action="#{bancoPerguntasMBean.removeAlternativaQuiz(alternativa)}">
                                <f:ajax render="quizAlternativasListPanel" execute="@this"
                                    resetValues="true" />
                                <p:confirm header="Confirmação"
                                    message="Deseja realmente apagar essa alternativa?"
                                    icon="ui-icon-alert" />
                            </p:commandButton>
                        </p:column>
                    </p:dataTable>
                </p:panel></td>
        </tr>
        <tr>
            <td><p:panel id="quizBotaoCriarPanel" styleClass="painelLimpo">
                    <p:commandButton id="btnCriarQuiz" value="Criar Questão Quiz"
                        styleClass="btnCriarQuestao"
                        actionListener="#{bancoPerguntasMBean.criarNovaQuestaoQuiz}"
                        onclick="return validaInputObrigatorio('inputEnunciadoQuiz');"
                        resetValues="true" update="modalMecanica">
                        <f:ajax render="quizAlternativasListPanel" execute="@this"
                            resetValues="true" />
                        <p:confirm header="Confirmação"
                            message="Deseja realmente adicionar essa questão de Quiz na avaliação?"
                            icon="ui-icon-alert" />
                    </p:commandButton>
                </p:panel></td>
        </tr>
    </table>
</h:form>
<p:confirmDialog global="true" responsive="true" width="500"
    styleClass="painelConfirmacao">
    <p:commandLink styleClass='bt bt-p bt-action ui-confirmdialog-yes'
        value="Sim" />
    <p:commandLink styleClass='bt bt-p bt-del ui-confirmdialog-no'
        value="Não" />
</p:confirmDialog>

On the button, it has no action in MB

<p:commandButton value="Criar Alternativa"
                        action="#{bancoPerguntasMBean.criarNovaAlternativaQuiz}">
                        <f:ajax render="quizAlternativasPanel" execute="@this"
                            resetValues="true" />
                    </p:commandButton>

This is due to the fact that he is already inside a form, la no dialog?

This is my method in MB that should be called when clicking on commandButton

public void criarNovaAlternativaQuiz() {

    BancoPerguntasMecanicaQuiz objMecanicaQuiz = new BancoPerguntasMecanicaQuiz();  

    try {

        if (StringUtils.isNullOrEmpty(this.novaAlternativaQuiz)) {
            return;
        }

        for (BancoPerguntasMecanicaQuiz alternativa : this.listaMecanicaQuiz) {
            if (alternativa.getAlternativa().equals(this.novaAlternativaQuiz)) {
                String erro = "Essa alternativa já foi incluída";
                RequestContext.getCurrentInstance().execute("exibeErroNovaAvaliacao('" + erro + "');");
                return;
            }
        }


        objMecanicaQuiz.setAlternativa(this.novaAlternativaQuiz);
        objMecanicaQuiz.setCorreta(this.novaAlternativaCorretaQuiz ? 1 : 0);
        this.listaMecanicaQuiz.add(objMecanicaQuiz);

        this.novaAlternativaQuiz = null;
        this.novaAlternativaCorretaQuiz = false;

    } catch (Exception e) {
        String erro = ExceptionUtil.trataErrosCriacaoQuestaoQuiz(e);
        RequestContext.getCurrentInstance().execute("exibeErroNovaAvaliacao('" + erro + "');");

    }

}   
  • Put the Managedbean code, please

  • Hello Igor, I just edited the post by adding Métod to the bean that should be called.

  • Cool. It shouldn’t be that, but it changes from action for actionListener. The action is used when redirecting after the method action. This way, the method should return a String. The actionListener nay.

1 answer

0

Try this:

<p:commandButton value="Criar Alternativa" action="#{bancoPerguntasMBean.criarNovaAlternativaQuiz}" update="quizAlternativasPanel" partialSubmit="true" process="@form" resetValues="true" />

Browser other questions tagged

You are not signed in. Login or sign up in order to post.