1

I'm trying to update a value that is inside a h:dataTable from a Bootstrap Modal using pure JSF 2.2 with CDI.

Here is the dataTable:

<h:dataTable id="tabNotas" value="#{notaBean.notas}" var="nota"
                        styleClass="table table-hover">
                        <h:column>
                            <f:facet name="header">
                                <h:outputText value="ALUNO" />
                            </f:facet>
                            <h:outputText value="#{nota.aluno.nome}" />
                        </h:column>
                        <h:column>
                            <f:facet name="header">
                                <h:outputText value="Disciplina" />
                            </f:facet>
                            <h:outputText value="#{nota.disciplina.nome}" />
                        </h:column>
                        <h:column>
                            <f:facet name="header">
                                <h:outputText value="TIPO" />
                            </f:facet>
                            <h:outputText value="#{nota.tipo.descricao}" />
                        </h:column>
                        <h:column>
                            <f:facet name="header">
                                <h:outputText value="NOTA" />
                            </f:facet>
                            <h:outputText value="#{nota.valor}" />
                        </h:column>
                        <h:column>

                            <h:commandLink class="btn btn-primary" value="Alterar">
                                <f:passThroughAttribute name="data-toggle" value="modal" />
                                <f:passThroughAttribute name="data-target" value="#myModal" />
                                <f:ajax listener="#{notaBean.editarNota(nota)}"
                                    render="formModalNotas" />
                            </h:commandLink>
                        </h:column>
                    </h:dataTable>

And here is the modal:

<div id="myModal" class="modal fade">
                <h:form id="formModalNotas" role="form">
                    <div class="modal-dialog">
                        <div class="modal-content">
                            <h:messages showSummary="true" errorClass="alert alert-danger"
                                infoClass="alert alert-success" for="formModalNotas" />
                            <input type="hidden" name="id" value="#{notaBean.nota.aluno.id}" />
                            <div class="modal-header">
                                <button type="button" class="close" data-dismiss="modal"
                                    aria-hidden="true"></button>
                                <h4 class="modal-title">Alterar</h4>
                            </div>
                            <div class="modal-body">
                                <div class="form-group ">
                                    <label for="tipoNota">Tipo da Nota:</label>
                                    <h:inputText value="#{notaBean.nota.tipo.descricao}"
                                        styleClass="form-control" id="tipoNota" disabled="true" />
                                </div>
                                <div class="form-group ">
                                    <label for="nota">Nota:</label>
                                    <h:inputText value="#{notaBean.nota.valor}"
                                        styleClass="form-control" id="nota" />
                                </div>
                            </div>
                            <div class="modal-footer">
                                <button type="button" class="btn btn-default"
                                    data-dismiss="modal">Close</button>
                                <h:commandButton value="Salvar" action="#{notaBean.alterar}"
                                    styleClass="btn btn-primary">
                                </h:commandButton>
                            </div>
                        </div>
                    </div>
                </h:form>
            </div>

The modal works just fine, the problem is in the action="#{notaBean.alterar}" that is never called.

Am I doing something wrong? Has anyone done this before?

EDIT

I'm using Tomcat 8. Here is the code of NotaBean, I've changed to ViewScoped still nothing =/

@Named(value = "notaBean")
@ViewScoped
public class NotaBean implements Serializable {

private static final long serialVersionUID = 5134990385063835570L;

@Inject
private NotaDao notaDao;

@Inject
private Dao<Curso> daoCurso;

@Inject
private Dao<Disciplina> disciplinaDao;

@Inject
private Nota nota;

private Aluno aluno;

private List<Disciplina> disciplinas;

@PostConstruct
public void init() {
    nota.setDisciplina(new Disciplina());
}

public Nota getNota() {
    return nota;
}

public void setNota(Nota nota) {
    this.nota = nota;
}

public Aluno getAluno() {
    return aluno;
}

public void setAluno(Aluno aluno) {
    this.aluno = aluno;
}

public List<Disciplina> getDisciplinas() {
    if (aluno == null || aluno.getCurso().getId() == null) {
        disciplinas = null;
    } else {
        Curso cursoSelecionado = daoCurso.buscaPorId(aluno.getCurso()
                .getId());
        disciplinas = cursoSelecionado.getDisciplinas();
    }

    return disciplinas;
}

public void setDisciplinas(List<Disciplina> disciplinas) {
    this.disciplinas = disciplinas;
}

@Transacional
public void cadastrar() {
    try {
        nota.setAluno(aluno);
        nota.setDisciplina(disciplinaDao.buscaPorId(nota.getDisciplina()
                .getId()));
        validar(nota);
        notaDao.adiciona(nota);
        FacesContext facesContext = FacesContext.getCurrentInstance();
        FacesMessage facesMessage = new FacesMessage(
                "Nota cadastrada com sucesso!");
        facesMessage.setSeverity(FacesMessage.SEVERITY_INFO);
        facesContext.addMessage(null, facesMessage);
    } catch (Exception e) {
        e.printStackTrace();
        FacesContext facesContext = FacesContext.getCurrentInstance();
        FacesMessage facesMessage = new FacesMessage(e.getMessage());
        facesMessage.setSeverity(FacesMessage.SEVERITY_ERROR);
        facesContext.addMessage(null, facesMessage);
    }
}

private void validar(Nota nota) throws Exception {
    if (nota.getValor() == null) {
        throw new Exception("Valor da nota � obrigat�rio");
    }

    if (nota.getValor() < 0 || nota.getValor() > 10) {
        throw new Exception("Valor da nota deve ser entre 0 e 10");
    }

    for (Nota notaAluno : aluno.getNotas()) {
        if (notaAluno.getDisciplina().equals(nota.getDisciplina())) {
            if (notaAluno.getTipo().equals(nota.getTipo())) {
                throw new Exception("Nota de " + nota.getTipo()
                        + " para a disciplina " + notaAluno.getDisciplina()
                        + " j� cadastrada");
            }
        }
    }
}

public List<TipoNota> getTiposNota() {
    return Arrays.asList(TipoNota.values());
}

public List<Nota> getNotas() {
    return notaDao.buscarNotasAluno(aluno.getId());
}

public void editarNota(Nota nota) {
    this.nota = nota;
}

@Transacional
public void alterar() {
    try {
        validar(nota);
        notaDao.atualiza(nota);
        FacesContext facesContext = FacesContext.getCurrentInstance();
        FacesMessage facesMessage = new FacesMessage(
                "Nota alterada com sucesso!");
        facesMessage.setSeverity(FacesMessage.SEVERITY_INFO);
        facesContext.addMessage(null, facesMessage);
    } catch (Exception e) {
        e.printStackTrace();
        FacesContext facesContext = FacesContext.getCurrentInstance();
        FacesMessage facesMessage = new FacesMessage(e.getMessage());
        facesMessage.setSeverity(FacesMessage.SEVERITY_ERROR);
        facesContext.addMessage(null, facesMessage);
    }
}
}
  • could you give your server and version and the source code of `NotaBean`? – Antoine Sabot-Durand Jul 08 '15 at 16:31
  • Check if on the cliend-side the html form is IN the 'dialog' (or at least that what becomes the dialog) if it is fully rendered... – Kukeltje Jul 08 '15 at 17:08
  • @AntoineSabot-Durand I'vew edited the question with the NotaBean source. I'm using Tomcat 8 – Felipe Moraes Jul 08 '15 at 17:34
  • @Kukeltje the dialog has a form in it. the dialog renders ok it just doesn't call the action. – Felipe Moraes Jul 08 '15 at 17:35
  • Probably because CDI wasn't activated. What is your CDI implementation and version. Why not using a Java EE server like TomEE, Glassfish or wildfly, it'll make your life easier. – Antoine Sabot-Durand Jul 09 '15 at 08:22
  • @AntoineSabot-Durand CDI is activated because I am using it in other parts of the project...the implementation I'm using is WELD 2.2.14...And I've changed the bean to ViewScoped and still nothing... – Felipe Moraes Jul 09 '15 at 16:28
  • Check all these... Point 3 maybe? http://stackoverflow.com/questions/2118656/commandlink-commandbutton-ajax-backing-bean-action-listener-method-not-invoked – Kukeltje Jun 19 '16 at 20:36

1 Answers1

0

The problem will be solved if you put the <h:form></h:form> out side the modal like: ​

<h:form>
.... modal.....
</h:form>

As per me inside a modal the command button should be use with <f:ajax></f:ajax> else it will reload the page.

Fabian N.
  • 3,807
  • 2
  • 23
  • 46
  • ??? I cannot see why this would help. And yes abesence of `f:ajax` does that, but that is a non-related issue – Kukeltje Jun 19 '16 at 19:33
  • It worked for me, I think by keeping form out side the modal form will rendered in the html (if u kept it inside it may not render and conflict with your other forms or its also working if i keeping the form inside modal-content) so h:commandButton worked. – Subrat Mishra Aug 10 '16 at 10:01
  • Did you by accident have a nested form? – Kukeltje Aug 10 '16 at 10:17