Question

I'm trying to create a selectManyCheckbox feature in my application, but now I'm in "converter problem". To take care this, I'm trying to use Omnifaces that already have a converter to objects.

My solution is based on this and this question (both answered by BalusC).


Don't know if it helps, but he is my view code:

<h:selectManyCheckbox style="margin-bottom: 40px;" id="disciplinas" value="#{cursoMBean.listaDisciplinasDoCurso}" converter="omnifaces.SelectItemsConverter">
    <f:selectItems value="#{cursoMBean.listaTodasDisciplinas}" var="disciplina" itemValue="#{disciplina}" itemLabel="#{disciplina.nome}"/>
</h:selectManyCheckbox>

And my MBean:

private static ArrayList<Disciplina> listaTodasDisciplinas;
private static ArrayList<Disciplina> listaDisciplinasDoCurso;

public ArrayList<Disciplina> getListaTodasDisciplinas() {
    return listaTodasDisciplinas;
}

public void setListaTodasDisciplinas(
        ArrayList<Disciplina> listaTodasDisciplinas) {
    CursoMBean.listaTodasDisciplinas = listaTodasDisciplinas;
}

public ArrayList<Disciplina> getListaDisciplinasDoCurso() {
    return listaDisciplinasDoCurso;
}

public void setListaDisciplinasDoCurso(
        ArrayList<Disciplina> listaDisciplinasDoCurso) {
    CursoMBean.listaDisciplinasDoCurso = listaDisciplinasDoCurso;
}

Disciplina:

public class Disciplina {

    private int id;
    private String nome;

    public Disciplina(int id, String nome) {
        this.id = id;
        this.nome = nome;
    }

    public Disciplina() {
    }

    // Methods
    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getNome() {
        return nome;
    }

    public void setNome(String nome) {
        if (!(nome.isEmpty() || nome == " " || nome == "  ")){
            this.nome = nome;
        }
    }

    @Override
    public String toString() {
        return nome;
    }

}

My problem is: this actually don't works. When I select some checkbox and submit, this create a new Curso but the arraylist of selected Disciplina still empty. I think the problem is that JSF can't find Omnifaces converter. This is my HTML tag in view:

<html xmlns:h="http://java.sun.com/jsf/html"
    xmlns:f="http://java.sun.com/jsf/core"
    xmlns:ui="http://java.sun.com/jsf/facelets"
    xmlns:p="http://primefaces.org/ui"
    xmlns:o="http://omnifaces.org/ui"
    xmlns:of="http://omnifaces.org/functions">

When I hover the "converter" in selectManyCheckbox appears a warning:

'omnifaces.SelectItemsConverter' converter id is not registered.

I putted the Omnifaces JAR inside Web-Inf/lib. To me, everything is okay, why Omnifaces don't populate my ArrayList with the selected items?

Edit

This is the button to submit the form with the checkboxes:

<h:commandButton id="enviar" styleClass="btn btn-lg btn-success pull-right" value="Adicionar" action="#{cursoMBean.cadastrar}">
    <f:ajax event="click" onevent="insert.hide()" render=":meuForm:minhaTabela"
    listener="#{cursoMBean.cadastrar}" />
</h:commandButton>

And here is the called method:

public String cadastrar() {

    Curso curso = new Curso();
    System.out.println("Check if listaDisciplinasDoCurso have something inside): " + listaDisciplinasDoCurso.size() +"\n");
    for (Disciplina d : listaDisciplinasDoCurso) {
        System.out.println(d);
    }


    if (!(this.getNome().isEmpty() || this.getNome() == " " || this
            .getNome() == "  ")) {

        curso.setNome(this.getNome());

        // Clearing the listaDisciplinasDoCurso 
        listaDisciplinasDoCurso = new ArrayList<Disciplina>();

        // Adding course to database
        controleCurso.adicionar(curso);

        System.out.println("Inserted. " + curso.toString());
    } else {
        System.out.println("Error: Not inserted. " + curso.toString());
    }

    limparCampos();
    atualizarListagem();

    return null;
}

Edit 2

My newest code, with two forms:

<h:form id="inserirDisciplina">
    <div class="form-group">
        <div class="col-md-10">
            <h:inputText styleClass="form-control" id="disciplina" value="#{cursoMBean.nome}" valueChangeListener="#{cursoMBean.atualizarListagemPesquisa}">
                <f:ajax event="keyup" render=":meuForm:minhaTabela" />
            </h:inputText>
        </div>
        <div class="col-md-2">
            <h:commandButton value="Adicionar" styleClass="btn btn-md btn-success" process="disciplina" partialSubmit="true">
                <p:ajax event="click" update=":meuForm:display" render=":meuForm:dialog" partialSubmit="true" process="disciplina" oncomplete="PF('insert').show();" onerror="alert('erro');" />
            </h:commandButton>
        </div>
    </div>
</h:form>

<p:messages autoUpdate="true" />

<p:dialog id="dialog" header="Inserir Curso" widgetVar="insert"
                resizable="false" modal="true" width="600" height="500"
                hideEffect="clip" closeOnEscape="true">

<h:form>
    <h:panelGrid id="display" styleClass="col-lg-10 center" style="margin-top: 10px; margin-bottom: 15px;">
        <label for="nome">Nome:</label>
        <p:inputText styleClass="form-control adicionar" id="nome" value="#{cursoMBean.nome}">

        </p:inputText>
    </h:panelGrid>

    <h:panelGrid styleClass="col-lg-10 center">
        <p:columnGroup>
            <label for="disciplinas">Disciplinas do Curso:</label>
            <h:selectManyCheckbox style="margin-bottom: 40px;" id="disciplinas" value="#{cursoMBean.listaDisciplinasDoCurso}" converter="omnifaces.SelectItemsConverter">
                <f:selectItems value="#{cursoMBean.listaTodasDisciplinas}" var="disciplina" itemValue="#{disciplina}" itemLabel="#{disciplina.nome}" />
            </h:selectManyCheckbox>
        </p:columnGroup>
    </h:panelGrid>

    <h:panelGrid styleClass="col-lg-10 center">
        <p:columnGroup>
            <h:commandButton id="enviar" styleClass="btn btn-lg btn-success pull-right" value="Adicionar" action="#{cursoMBean.cadastrar}">
                    <f:ajax event="click" execute="@form" onevent="insert.hide()" render=":meuForm:minhaTabela" listener="#{cursoMBean.cadastrar}" />
            </h:commandButton>
        </p:columnGroup>
    </h:panelGrid>
</h:form>
</p:dialog>
Was it helpful?

Solution

As to the Eclipse warning mentioned in the title,

'omnifaces.SelectItemsConverter' converter id is not registered

just ignore it. It's actually registered via a @FacesConverter annotation. It's only the IDE who's not smart enough to detect the @FacesConverter annotated classes in JARs deployed in /WEB-INF/lib. It's only looking for <converter> entries in faces-config.xml. Try to actually run the webapp project. If the converter wasn't properly registered, then you should have gotten the following exception:

javax.faces.FacesException: Expression Error: Object named: omnifaces.SelectItemsConverter not found


Coming back to your concrete problem, those static properties aren't right. Remove those static modifiers. Also, the <f:ajax> executes by default the current component, as in

<f:ajax execute="@this">

You need to specify it to @form if you intend to execute the entire form

<f:ajax execute="@form">

Also, the onevent="insert.hide()" is wrong. The onevent attribute should point to a function reference, not perform a function call. The function reference is in turn called three times per ajax request. Just use <h:commandButton onclick> for that instead.


Unrelated to the concrete problem, also get rid of event="click" it's the default already. There's no need to repeat the defaults.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top