0

我想在 JSF:DataTable 中显示一个表。我成功地将表从数据库检索到用户列表类型,其中“用户”是我的 pojo 类。现在我在数据表上显示它时遇到问题,某些列(如 FName、LName、Pwd)显示正确,但是当我添加其他列如“Note”“Email”时,它给了我这个错误

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> 

这是我用来从数据库中检索数据的 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;
    }

}
4

1 回答 1

3

字段必须是 likeThis 而不是 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">

相关资料:

于 2013-09-29T02:05:50.357 回答