Is there a restriction of jsf 2.2.5 with the Collection interface?

Asked

Viewed 92 times

0

I am running an application with jsf 2.2.5.
I used the component h:dataTable to display an object collection on the screen. I added this two component h:commandLink, one that must execute an editing method and another removal method to interact with the respective object in each iteration of the collection.

If I step into the component h:dataTable one TreeMap<> which is recovered through a Managed bean, of a static attribute of a given class, it works normally. However, when you filter that collection and return a List<> with part of the objects of the collection (this is assigned to an attribute of the Managed bean before being sent to view) both h:commandLink cease to function, the methods to which they are associated are not even executed.

Follow code from view:

...
<h:form acceptcharset="ISO-8859-1" id="lista" styleClass="col-xs-12 box" rendered="#{gerenciarLivro.resultadoDaPesquisa.size() > 0}">
    <h:dataTable  value="#{gerenciarLivro.resultadoDaPesquisa}"  var="livro"  >
        <h:column >
            <fieldset >
                <div class="col-xs-10 col-sm-10 col-md-11 pull-left">
                    <div class="col-xs-12 col-sm-12">
            <label>
                <strong>#{livro.titulo}</strong>
            </label>
                    </div>
                    <div class="col-xs-6 col-sm-6">
            <label class="text-info" >#{livro.autor}</label>
                    </div>
                    <div class="col-xs-6 col-sm-6">
            <label>#{livro.editora}</label>
                    </div>
                    <div class="col-xs-6 col-sm-3">
            <label>#{livro.genero}</label>
                    </div>
                    <div class="col-xs-6 col-sm-3">
            <label>#{livro.ISBN}</label>
                    </div>
                    <div class="col-xs-6 col-sm-3">
            <label>#{livro.ano}</label>
                    </div>
                    <div class="col-xs-6 col-sm-3">
            <label>#{livro.edicao}</label>
                    </div>
                </div>
                <div class="col-xs-2 col-sm-2 col-md-1 pull-right">
                    <div class="plus">
            <i class="glyphicon glyphicon-plus"> </i>
                    </div>
                </div>
                <div class="col-xs-12 sub hide">
                    <div class="form-group col-xs-4">
            <h:commandLink styleClass="btn btn-xs btn-inverse col-xs-12" action="#{gerenciarLivro.removerLivro(livro)}">
                <f:ajax />
                <i class="glyphicon glyphicon-trash pull-left"> </i> Remover
            </h:commandLink>
                    </div>
                    <div class="form-group col-xs-4">
            <h:commandLink styleClass="btn btn-xs btn-primary col-xs-12" action="#{gerenciarLivro.editarLivro()}">
                <f:ajax />
                <i class="glyphicon glyphicon-edit pull-left"> </i> Editar
            </h:commandLink>
                    </div>
                    <div class="form-group col-xs-4">
            <button class="btn btn-xs btn-primary col-xs-12" >
                <i class="glyphicon glyphicon-book pull-left"></i> Fila de espera
            </button>
                    </div>
                </div>
            </fieldset>
        </h:column>
    </h:dataTable>
</h:form>
...

Follow the code of the Managed bean:

...
@Named(value = "gerenciarLivro")
@RequestScoped
public class GerenciarLivroMB implements Serializable {

private String ISBN;
private String titulo;
private String autor;
private String editora;
private String genero;
private String ano;
private String edicao;
@Inject
private ControleDeLivro controle;
private List<Livro> resultadoDaPesquisa;

public void adicionarLivro() {
    controle.adicionar(construirLivro());
    limparDados();
}

public String removerLivro(Livro l) {
    controle.remover(l);
    resultadoDaPesquisa.remove(l);
    return ("removerLivro");
}

public String editarLivro(Livro l) {
    return ("editarLivro");
}

public Collection<Livro> getLivros() {
    return Repositorio.livros();
}

public Collection<Livro> getResultadoDaPesquisa() {
    return resultadoDaPesquisa;
}

public void pesquisarLivro() {
    if (resultadoDaPesquisa != null) {
        resultadoDaPesquisa.clear();
    }
    resultadoDaPesquisa = controle.pesquisar(construirLivro());
    limparDados();
}

private void limparDados() {
    ISBN = "";
    titulo = "";
    autor = "";
    editora = "";
    genero = "";
    ano = "";
    edicao = "";
}

private Livro construirLivro() {
    Livro l = new Livro();
    if (ISBN != null && !ISBN.isEmpty()) {
        l.setISBN(ISBN);
    }
    if (titulo != null && !titulo.isEmpty()) {
        l.setTitulo(titulo);
    }
    if (autor != null && !autor.isEmpty()) {
        l.setAutor(autor);
    }
    if (editora != null && !editora.isEmpty()) {
        l.setEditora(editora);
    }
    if (genero != null && !genero.isEmpty()) {
        l.setGenero(genero);
    }
    if (ano != null && !ano.isEmpty()) {
        l.setAno(ano);
    }
    if (edicao != null && !edicao.isEmpty()) {
        l.setEdicao(edicao);
    }
    return l;
}
...
outros getters e setters.

When I use the collection coming from this class everything works perfectly:

public class Repositorio {

    private static TreeSet<Livro> livros;
    private static List<Emprestimo> emprestimo;
    private static Deque<Emprestimo> devolucao;

    public static TreeSet<Livro> livros() {
        if (livros == null) {
            livros = new TreeSet<>();
        }
        return livros;
    }

    public static List<Emprestimo> emprestimo() {
        if (emprestimo == null) {
            emprestimo = new ArrayList<>();
        }
        return emprestimo;
    }

    public static Deque<Emprestimo> devolucao() {
        if (devolucao == null) {
            devolucao = new ArrayDeque<>();
        }
        return devolucao;
    }
}
  • Tibere, the dataTable displays filter items? Only actions do not work? How is the filter done? Any action or event that of the reRender in the dataTable? Did you ever see if there is an error in the console of the application server or browser console? The filter preserves the collection type?

  • Hi @Wakin. O dataTable displays all items regardless of which collection they have come from. Really only the actions do not work, but only in case I use the collection that is as attribute of bean. The filter is made on a copy of the original collection coming from the class Repositorio mentioned. Itero on this copy and remove all objects that are not compatible with the data used as filter parameters. The two mentioned collections are parameterized with the same object type (Book class). This collection is returned to the bean and then to the view.

No answers

Browser other questions tagged

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