0

this one is my jsf page in which i have tried a very good to meet the requirement on the properties of java bean object. I have checked all the problem in this website but noon of seem to help. I dont know what is problem with my code

    <h:dataTable value="#{contact.getPeopleList()}" var="data">

        <h:column>
            <f:facet name="header">ID</f:facet>
            <h:outputText value="#{data.id}"/>
        </h:column>
        <h:column>
            <f:facet name="header">Name</f:facet>
            <h:outputText value="#{persons.name}"/>
        </h:column>
        <h:column>
            <f:facet name="header">Mobile Number</f:facet>
            #{persons.mobilenumber}
        </h:column>
        <h:column>
            <f:facet name="header">Phone Number</f:facet>
            #{persons.telephone}
        </h:column>
        <h:column>
            <f:facet name="header">Address</f:facet>
            #{persons.address}
        </h:column>

    </h:dataTable>
</h:body>

This one is my Contact CLass in which i have inject Person class to get the properties of Person class

        @Named
        @RequestScoped
        public class Contact {
            private Person person;

            @PostConstruct
            public void init(){
                this.person=new Person();

            }

            public Person getPerson() {
                return person;
            }

            public void setPerson(Person person) {
                this.person = person;
            }



            public String onSubmit(){
                ContactDAO doaContact=new ContactDAO();
                doaContact.insertContact(person);


                return "contactpage";
            }
            public List<Person> getPeopleList(){
                ContactDAO doaContact=new ContactDAO();
                List<Person> personList =doaContact.getAll();
                return personList;
            }
        }


            package np.com.drose.beans;

            import java.util.List;
            import np.com.drose.doa.ContactDAO;



            public class Person {
                private int id;
                private String name;
                private int mobilenumber;
                private int telephone;
                private String address;


                public Person() {

                }

                public Person(int id, String name, int mobilenumber, int telephone, String address) {
                    this.id = id;
                    this.name = name;
                    this.mobilenumber = mobilenumber;
                    this.telephone = telephone;
                    this.address = address;
                }


                public Person(String name, int mobilenumber, int telephone, String address) {
                    this.name = name;
                    this.mobilenumber = mobilenumber;
                    this.telephone = telephone;
                    this.address = address;

                }

                public int getId() {
                    return id;
                }

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

                public String getName() {
                    return name;
                }

                public void setName(String name) {
                    this.name = name;
                }

                public int getMobilenumber() {
                    return mobilenumber;
                }

                public void setMobilenumber(int mobilenumber) {
                    this.mobilenumber = mobilenumber;
                }

                public int getTelephone() {
                    return telephone;
                }

                public void setTelephone(int telephone) {
                    this.telephone = telephone;
                }

                public String getAddress() {
                    return address;
                }

                public void setAddress(String address) {
                    this.address = address;
                }   
            }

here i get confused in the Data access object class where i cant seem to find what i did wrong and why my code is not rendering data from database

 public List<Person> getAll(){
                    List<Person> contactList= new LinkedList<>();
                    String sql="Select * from contact";
                    try{
                        conn = DataConnection.getConnetion();
                        ps=conn.prepareStatement(sql);
                        rs=ps.executeQuery();
                        while(rs.next()){
                           Person person=new Person(rs.getInt("id"),rs.getString("name"),rs.getInt("mobilenumber"),rs.getInt("telephone"),rs.getString("address"));
                           contactList.add(person);
                        }

                    }catch(Exception EX){
                        EX.printStackTrace();
                    }
                    return contactList;
                }
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
Bibek Shakya
  • 1,233
  • 2
  • 23
  • 45
  • To exclude one and other, rightclick page in webbrowser and do "View page source". What exactly do you see? Do you see the unparsed JSF source code or its generated HTML output? And, do you have debugged the Java code to see if it's actually invoked or not? – BalusC May 16 '16 at 08:41
  • /contactpage.xhtml @10,66 value="#{contact.getPeopleList}": The class 'np.com.drose.beans.Contact' does not have the property 'getPeopleList'. – Bibek Shakya May 16 '16 at 08:57

1 Answers1

1

to render datatable in jsf,you need to have listas attribute of Bean class.For example :

           @Named
        @RequestScoped
        public class Contact {
            private Person person;
            private List<Person>personList;


            @PostConstruct
            public void init(){
                this.person=new Person();

            }

            public Person getPerson() {
                return person;
            }

            public void setPerson(Person person) {
                this.person = person;
            }

            public List<Person> getPersonList() {
        return personList;
    }



    public void setPersonList(List<Person> personList) {
        this.personList = personList;
    }

            public String onSubmit(){
                ContactDAO doaContact=new ContactDAO();
                doaContact.insertContact(person);


                return "contactpage";
            }
            public List<Person> getPeopleList(){
                ContactDAO doaContact=new ContactDAO();
               personList =doaContact.getAll();
                return personList;
            }
}

and in your html file you should write like this :

<h:dataTable value="#{contact.personList}" var="data">

please check like that.

sawyinwaimon
  • 739
  • 1
  • 6
  • 14
  • does not have the property 'personList'. still throwing this error – Bibek Shakya May 16 '16 at 09:04
  • you should have property personList in your bean class.then provide getter/setter for this personList.and assign value from Database to personList in getPeopleList()method as I show u.then in jsf page,write like that . – sawyinwaimon May 16 '16 at 09:06
  • I have tried the code sequence as per you say still have same error – Bibek Shakya May 16 '16 at 09:11
  • so within init()method,can you try like this? init(){ this.person=new Person(); ContactDAO doaContact=new ContactDAO(); personList =doaContact.getAll();}and you should still provide personList attribute with getter/setter method and bind in jsf page that I already told.and please change @ManagedBean annotation in your class. – sawyinwaimon May 16 '16 at 09:21
  • By the way,do you check doaContact.getAll()actually return data from DB by debugging? – sawyinwaimon May 16 '16 at 09:45
  • Not able to submit breakpoint FieldBreakpoint np.com.drose.beans.Person.personList, reason: Field 'personList' does not exist in class np.com.drose.beans.Person. The code is not been able to access the database object – Bibek Shakya May 16 '16 at 09:55
  • i did debug my code but code is not working from this part conn = DataConnection.getConnetion(); ps=conn.prepareStatement(sql); rs=ps.executeQuery(); – Bibek Shakya May 16 '16 at 10:00