Wilson Ribeiro
Wilson Ribeiro

Reputation: 373

JSF reload the same function several times

I have a problem with JSF.

I make a page with JSF and dataTable of primefaces, but I realized that when I use a var into datatable, JSF reload a same get method many times. I don't know if because the JSF or my Program, someone can help me?

 <p:dataTable var="Usuario" value="#{usuarioBean.listaUsuario}"
                             paginator="true" rows="10" selection="#{usuarioBean.usuario}"
                             rowKey="#{Usuario.id}"
                             id="dataTable"
                             paginatorPosition="bottom">
                    <p:column headerText="ID" style="width: 10px">
                        <h:outputText value="#{Usuario.id}"/>
                    </p:column>
                    <p:column headerText="Nome">
                        <h:outputText value="#{Usuario.nome}"/>
                    </p:column>
                    <p:column headerText="E-mail">
                        <h:outputText value="#{Usuario.email}"/>
                    </p:column>
                    <p:column headerText="Telefone" style="width: 10px">
                        <h:outputText value="#{Usuario.telefone}"/>
                    </p:column>

                    <p:column headerText="Editar" style="width: 10px;">
                        <p:commandLink  id="btnEditar" action="#{usuarioBean.ChamareditarUsuario()}" ajax="false" title="Editar">     
                            <h:graphicImage  value="/resources/img/editar.png" style="position: relative; top: 25%; left: 25%;" />     
                            <f:setPropertyActionListener value="#{Usuario}" target="#{usuarioBean.usuario}" /> 
                        </p:commandLink>
                    </p:column>

                    <p:column headerText="Excluir" style="width: 10px;">                                                 
                        <p:commandLink  id="btnDeletar" title="Deletar" action="#{usuarioBean.deletarUsuario()}" update="dataTable">     
                            <h:graphicImage  value="/resources/img/deletar.png" style="position: relative; top: 25%; left: 25%;"/>                                                
                            <f:setPropertyActionListener value="#{Usuario}" target="#{usuarioBean.usuario}" /> 
                        </p:commandLink>
                    </p:column>
                    <p:column  selectionMode="single" width="1%"/>
                </p:dataTable>




@ManagedBean
@RequestScoped
public class UsuarioBean {

private Usuario usuario = new Usuario();
private String campo;
private String valor;
private List<Usuario> listaUsuario;
private String acesso;
private List<Acesso> listaAcesso;    

Upvotes: 1

Views: 136

Answers (2)

Jessai
Jessai

Reputation: 947

You can understand a little more in this question:

Why JSF calls getters multiple times

Basiclly, a getter is called several times, is part of the JSF lifecycle.

Cheers.

Upvotes: 2

dognose
dognose

Reputation: 20909

Show your usuarioBean - what Scope does it have?

If it has no Scope (or the wrong scope), the bean will be reconstructed, every time you access #usuarioBean.listaUsuario- which will happen 4 times per iteration in your example.

To avoid that, make it @RequestScoped, so it lives as long as the current Request.

Sidenode: h:datatable is not the primefaces component. it would be p:datatable with the right Namespace imported.

Upvotes: 0

Related Questions