0

I have a list of users that I want to display in a JSF page using the primefaces p:datatable. I have no exception, but I can't display the data that a have into the list,even the column titles are not shown, in fact, I user the 'getUserListe()'method to get the data from the database calling my service layer, to be sure of that, I added a sysprint() of some userDomain properties (my model class), when I access to the URL, I want to populate my datatable with the data from the UserListe, so I call it by value="#{userMB.userListe}", the problem is that the list is not displayed. but when the page is charged, with means that getUserListe() is called, in my console, the proprties are printed successfullty, someone has any idea about this ?? :

a part of my managed bean :

@ManagedBean(name="userMB")
@RequestScoped
public class UserManagedBean implements Serializable {

        private List<UserDomain> userListe ;
    public List<UserDomain> getUserListe() {
            this.userListe = new ArrayList<UserDomain>();
            this.userListe.addAll(getUserServicee().getAll());
            for(UserDomain u : userListe)
                System.out.println(u.getFirstName());
            return userListe;
        }

        public void setUserListe(List<UserDomain> userListe) {
            this.userListe = userListe;
        }

part of my jsf.xhtml file:

<h:form>
        <h:outputText value="USER's rights list: "></h:outputText>
        <p:dataTable id="userss" var="userr" value="#{userMB.userListe}" style="width: 10%">

            <h:column>
                    <f:facet name="header"><h:outputText value="FirstName" /></f:facet>                     
                    <h:outputText value="#{userr.firstName}"  />                    
            </h:column>

            <h:column>
                    <f:facet name="header"><h:outputText value="LastName" /></f:facet>                  
                    <h:outputText value="#{userr.lastName}"  />                 
            </h:column>

            <h:column>                  
                    <f:facet name="header"><h:outputText value="Action" /></f:facet>                    
                    <h:commandLink value="Edit" action="#{userMB.editAction(userr)}"  />                
            </h:column>

        </p:dataTable> 

        <h:commandButton value="OK" action="#{userMB.userProp}" />
    </h:form>

Note that my user has all the getters and setters of his properties setted correctly, thank you for help

maouven
  • 111
  • 2
  • 9
  • 1
    `` works with ``, change it to ``. Make your bean @ViewScoped and move the business logic of your `getUserListe` to another method, for this read [Between a jsf page and a managed bean, why the getter method is called twice](http://stackoverflow.com/a/2788213/1065197) – Luiggi Mendoza Aug 01 '12 at 14:40
  • Luiggi has it right, using a p:column will fix it, and you should be loading that data once, not for every request for the value. – Daniel B. Chapman Aug 01 '12 at 15:18
  • thank you @LuiggiMendoza for your answer, you are right, and Daniel, how can I load the data only once, is that by the "onload" preperty or what ?? Thank you very much again for your help. – maouven Aug 01 '12 at 15:56
  • To load the list only once, load the list in a method annotated @PostConstruct and keep the list in a member variable of UserManagedBean. Then, in "getUserListe()", just return the variable. The reason is that "getUserListe()" will be called manytimes by JSF when the page is rendered. – baraber Aug 01 '12 at 16:30
  • @maouven you can create a method that retrieves the data and call it in the bean constructor and in other methods like a commandButton action to refresh the data. Assuming this is your case, it would be better to make your bean ViewScoped, the RequestScoped will create the bean in every request (even ajax requests!). – Luiggi Mendoza Aug 01 '12 at 16:57
  • @baraber nice answer, there is a further explanation made by BalusC in the link (and the links there) I've posted in my comment. – Luiggi Mendoza Aug 01 '12 at 16:59

1 Answers1

0

I did as @luiggi said in his comment by changing the <h:column> by the <p:column> and it's work fine, so thank you. I also changed my managed bean from an @RequestScoped to an @ViewScoped one.

the other thing that I want to add and share with you , is that I did some research after reading the comments of this post, so I found an other solution that seems better than others to load the list only one time, It's by attaching a listener to a system event, so in my jsf file I add

<f:view>
    <f:event type="preRenderView" listener="#{userMB.loadUserListe}" />
...

the loadUserListe() method in my bean call the business logic code which instantiate the list of users. Of course, the jsf page is enclosed in an f:view. Note also that the use of this listener requires at least the JSF 2.0

thank you all for your help, and excuse me for my english, I hope this response helps others

maouven
  • 111
  • 2
  • 9