JSF Datatable不显示所有List字段(列)

时间:2013-09-29 01:46:01

标签: jsf datatable

我想在JSF中显示一个表:DataTAble。我成功地将表从数据库恢复到用户类型列表,其中“users”是我的pojo类。现在我有问题,在数据表上显示一些列,如FName,LName,Pwd,正确显示,但当我添加其他coulmns如“注”“电子邮件”,它给了我这个错误

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)

这是我的xhtml页面

<?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> 

这是我用于从DB

检索数据的PretechDataTableBean
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;


    }
}

这是我的用户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;
    }

}

1 个答案:

答案 0 :(得分:3)

字段必须像这样而不是LikeThis。只需将您的JSF代码更改为

即可
<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>

更新User类中的字段名称,以遵循正确的Java Bean命名约定。

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
}

除此之外,您当前的设计中还有其他错误:

  • 必须在托管bean的getter中拥有业务逻辑,而是利用@PostConstruct方法初始化要使用的必要数据。
  • 由于此bean看起来应该在用户停留在同一视图中时保持活动状态,因此最好将其装饰为@ViewScoped而不是@RequestScoped
  • 为您的班级和字段使用专有名称。例如,如果您有List<Something>字段,请为变量命名somethingList或类似名称,以便代码不言自明。

通过这些,您可以将托管bean更改为:

@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;
    }
}

由于字段在托管bean中更改了名称,因此您应该在相应的视图中对其进行相应编辑:

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

相关信息: