0

I want to display a table in JSF:DataTAble. I successfully retrived table from database to List of users type where "users" is my pojo class. Now I am having problem with displaying it on data table some of the columns like FName, LName, Pwd, displayed correctly but when i add other coulmns like "Note" "Email" it gives me this error

javax.servlet.ServletException: /dt.xhtml: Property 'Email' not found on type in.ali.pojo.users
    javax.faces.webapp.FacesServlet.service(FacesServlet.java:659)


root cause 

javax.el.ELException: /dt.xhtml: Property 'Email' not found on type in.ali.pojo.users
    com.sun.faces.facelets.compiler.TextInstruction.write(TextInstruction.java:88)
    com.sun.faces.facelets.compiler.UIInstructions.encodeBegin(UIInstructions.java:82)
    com.sun.faces.renderkit.html_basic.HtmlBasicRenderer.encodeRecursive(HtmlBasicRenderer.java:302)
    com.sun.faces.renderkit.html_basic.TableRenderer.renderRow(TableRenderer.java:385)
    com.sun.faces.renderkit.html_basic.TableRenderer.encodeChildren(TableRenderer.java:162)
    javax.faces.component.UIComponentBase.encodeChildren(UIComponentBase.java:894)
    javax.faces.component.UIComponent.encodeAll(UIComponent.java:1856)
    javax.faces.component.UIComponent.encodeAll(UIComponent.java:1859)
    javax.faces.component.UIComponent.encodeAll(UIComponent.java:1859)
    com.sun.faces.application.view.FaceletViewHandlingStrategy.renderView(FaceletViewHandlingStrategy.java:443)
    com.sun.faces.application.view.MultiViewHandler.renderView(MultiViewHandler.java:131)
    com.sun.faces.lifecycle.RenderResponsePhase.execute(RenderResponsePhase.java:120)
    com.sun.faces.lifecycle.Phase.doPhase(Phase.java:101)
    com.sun.faces.lifecycle.LifecycleImpl.render(LifecycleImpl.java:219)
    javax.faces.webapp.FacesServlet.service(FacesServlet.java:647)

here is my xhtml page

<?xml version='1.0' encoding='UTF-8' ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:h="http://java.sun.com/jsf/html"
       xmlns:f="http://java.sun.com/jsf/core"
       xmlns:ui="http://java.sun.com/jsf/facelets">
    <h:head>
        <title>Facelet Title</title>
    </h:head>
    <h:body>
<h:dataTable value="#{pretechDataTableBean.user}" var="users">
                 <h:column>
                    <f:facet name="header">Name</f:facet>
                    #{users.FName}
                      </h:column>
                 <h:column>
                    <f:facet name="header">Email</f:facet>
                    #{users.Email}
                </h:column>
                <h:column>
                    <f:facet name="header">Password</f:facet>
                    #{users.pwd}
                </h:column>

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

here is my PretechDataTableBean which i used for retrieving data from DB

package com.pretech; 
import in.ali.pojo.users;
import in.ali.util.HibernateUtil;

import java.util.List;

import org.hibernate.Session;
import org.hibernate.Transaction;



import java.util.ArrayList;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped; 
/**
*
* @author vinod
*/
@ManagedBean
@RequestScoped
public class PretechDataTableBean { 
    public PretechDataTableBean() {
    } 
    public List<users> getUser() {

        Session session = HibernateUtil.getSessionFactory().openSession();
        Transaction transaction = null;
        List<users> users =null;
        try
        {
            transaction = session.beginTransaction();
            users = session.createQuery("from users").list();   



        }
        catch(Exception e)
        {
            e.printStackTrace();
        }
        finally{
            session.close();
        }
        return users;


    }
}

This is my users pojo

package in.ali.pojo;

// Generated Sep 28, 2013 3:55:01 PM by Hibernate Tools 4.0.0

/**
 * users generated by hbm2java
 */
public class users implements java.io.Serializable {

    private long UserId;
    private String FName;
    private String LName;
    private long UserTypeId;
    private String UserName;
    private String Email;
    private String Pwd;
    private String Note;
    private boolean IsActive;

    public users() {
    }

    public users(long UserId) {
        this.UserId = UserId;
    }

    public users(long UserId, String FName, String LName, long UserTypeId,
            String UserName, String Email, String Pwd, String Note,
            boolean IsActive) {
        this.UserId = UserId;
        this.FName = FName;
        this.LName = LName;
        this.UserTypeId = UserTypeId;
        this.UserName = UserName;
        this.Email = Email;
        this.Pwd = Pwd;
        this.Note = Note;
        this.IsActive = IsActive;
    }

    public long getUserId() {
        return this.UserId;
    }

    public void setUserId(long UserId) {
        this.UserId = UserId;
    }

    public String getFName() {
        return this.FName;
    }

    public void setFName(String FName) {
        this.FName = FName;
    }

    public String getLName() {
        return this.LName;
    }

    public void setLName(String LName) {
        this.LName = LName;
    }

    public long getUserTypeId() {
        return this.UserTypeId;
    }

    public void setUserTypeId(long UserTypeId) {
        this.UserTypeId = UserTypeId;
    }

    public String getUserName() {
        return this.UserName;
    }

    public void setUserName(String UserName) {
        this.UserName = UserName;
    }

    public String getEmail() {
        return this.Email;
    }

    public void setEmail(String Email) {
        this.Email = Email;
    }

    public String getPwd() {
        return this.Pwd;
    }

    public void setPwd(String Pwd) {
        this.Pwd = Pwd;
    }

    public String getNote() {
        return this.Note;
    }

    public void setNote(String Note) {
        this.Note = Note;
    }

    public boolean isIsActive() {
        return this.IsActive;
    }

    public void setIsActive(boolean IsActive) {
        this.IsActive = IsActive;
    }

}
Luiggi Mendoza
  • 85,076
  • 16
  • 154
  • 332
Ali Asad
  • 147
  • 3
  • 11

1 Answers1

3

The fields must be likeThis instead of LikeThis. Just change your JSF code to

<h:dataTable value="#{pretechDataTableBean.user}" var="user">
    <h:column>
        <f:facet name="header">Name</f:facet>
        #{user.fName}
    </h:column>
    <h:column>
        <f:facet name="header">Email</f:facet>
        #{user.email}
    </h:column>
    <h:column>
        <f:facet name="header">Password</f:facet>
        #{user.pwd}
    </h:column>
</h:dataTable>

And update the field names in your User class to follow the proper Java Bean naming convention.

public class users implements java.io.Serializable {

    private long userId;
    private String fName;
    private String lName;
    private long userTypeId;
    private String userName;
    private String email;
    private String pwd;
    private String note;
    private boolean isActive;
    //constructor, getters and setters
}

Apart from this, there are other bugs in your current design:

  • You must not have business logic in the getters of your managed bean, instead take advantage of the @PostConstruct method to initialize the necessary data to be used.
  • Since this bean looks that should stay alive while the user stays in the same view, it will be better to decorate it as @ViewScoped instead of @RequestScoped.
  • Use proper names for your classes and fields. For example, if you have a List<Something> field, name your variable somethingList or similar in order that the code is self-explanatory.

From these, you can change your managed bean to:

@ManagedBean
@ViewScoped
public class PretechDataTableBean { 

    private List<users> userList;

    public PretechDataTableBean() {
    }

    @PostConstruct
    public void init() {
        Session session = HibernateUtil.getSessionFactory().openSession();
        Transaction transaction = null;
        List<users> users =null;
        try
        {
            transaction = session.beginTransaction();
            users = session.createQuery("from users").list();   



        }
        catch(Exception e)
        {
            e.printStackTrace();
        }
        finally{
            session.close();
        }
        return users;
    }

    public List<users> getUserList() {
        return this.user;
    }
}

Since the field changed its name in the managed bean, you should edit it accordingly in the respective view:

<h:dataTable value="#{pretechDataTableBean.userList}" var="user">

Related info:

Community
  • 1
  • 1
Luiggi Mendoza
  • 85,076
  • 16
  • 154
  • 332