JSF 视图未从数据库中恢复数据



我正在尝试从数据库中检索数据并将其放入数据表中的 JSF 视图中,但有一个Exception问题我无法解决。我将必要的jar复制到lib文件夹中,这是列表:

  • JSF-API.jar
  • JSF-IMPL.jar
  • JSTL-API-1.2.jar
  • JSTL-IMPL-1.2.jar

下面是 JSF View 的代码:listFournisseur.xhtml

<!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">
<head>
<link rel="stylesheet" type="text/css" href="../css/styles.css"/>
</head>
<h:body>
<h:form>
<h:dataTable  var="fournisseur" value="#{fournisseurBean.listFournisseurs}" border="1">
<h:column>
<f:facet name="header">
<h:outputLabel value="nom" />
</f:facet>
#{fournisseur.nomfour}
</h:column>
<h:column>
<f:facet name="header">
<h:outputLabel value="raison sociale" />
</f:facet>
#{fournisseur.raisonsfour}
</h:column>
<h:column>
<f:facet name="header">
<h:outputLabel value="Adresse" />
</f:facet>
 #{fournisseur.adressef}
</h:column>
</h:dataTable>
</h:form>
</h:body>
</html>

这是 ManagedBean 的代码:FournisseurBean.java

package com.fst.bibliotheque.beans;
import java.util.List;
import com.fst.bibliotheque.dao.FournisseurHome;
import com.fst.bibliotheque.persistance.Fournisseur;
import com.fst.bibliotheque.services.FournisseurService;
public class FournisseurBean {
private FournisseurService gestionFournisseur;
private List<Fournisseur> listFournisseurs;
    private int tailleListe;
public List<Fournisseur> getListFournisseurs() {
    if(gestionFournisseur==null)
        gestionFournisseur=new FournisseurService();
    listFournisseurs=gestionFournisseur.findAllList();
    tailleListe=listFournisseurs.size();
    return listFournisseurs;
}
public void setListFournisseurs(List<Fournisseur> listFournisseurs) {
    this.listFournisseurs = listFournisseurs;
}

    }

最后,这里是与层服务关联的代码:FournisseurServices.java其中是负责检索数据的方法。

package com.fst.bibliotheque.services;
import java.util.List;
import org.hibernate.Session;
import org.hibernate.Transaction;
import com.fst.bibliotheque.dao.FournisseurHome;
import com.fst.bibliotheque.dao.HibernateUtil;
public class FournisseurService {
private FournisseurHome dao;
public FournisseurService() { dao = new FournisseurHome() ; }
public List findAllList() { 
    Session session=HibernateUtil.getSessionFactory().getCurrentSession(); 
    Transaction tx= null ;
    List l=null;
    try{ 
        tx=session.beginTransaction(); 
        l=dao.findAll(); 
        tx.commit() ; 
        }catch(RuntimeException ex){
            if(tx!= null) tx.rollback();
            ex.printStackTrace() ; }
return l;   
}
}

函数 findAllList() 调用位于 dao 层中的 findAll() 函数:Fournisseurdao.java:

package com.fst.bibliotheque.dao;
// Generated Dec 7, 2013 2:15:19 AM by Hibernate Tools 3.4.0.CR1
import java.util.List;
import javax.naming.InitialContext;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.Criteria;
import org.hibernate.LockMode;
import org.hibernate.SessionFactory;
import org.hibernate.criterion.Order;
import com.fst.bibliotheque.persistance.Fournisseur;
import static org.hibernate.criterion.Example.create;
/**
* Home object for domain model class Fournisseur.
* @see com.fst.bibliotheque.dao.Fournisseur
* @author Hibernate Tools
*/
public class FournisseurHome {
private static final Log log = LogFactory.getLog(FournisseurHome.class);
private final SessionFactory sessionFactory = getSessionFactory();
protected SessionFactory getSessionFactory() {
    try {
        return HibernateUtil.getSessionFactory();
    } catch (Exception e) {
        log.error("Could not locate SessionFactory in JNDI", e);
        throw new IllegalStateException(
                "Could not locate SessionFactory in JNDI");
    }
}
public List findAll () { 
    Criteria crit     =sessionFactory.getCurrentSession().createCriteria(Fournisseur.class);
    return crit.list();
}
public List findAll (Order defaultOrder) { 
    Criteria crit = sessionFactory.getCurrentSession().createCriteria(Fournisseur.class);
    if (null != defaultOrder) crit.addOrder(defaultOrder);
    return crit.list();
}
}

执行后,我得到了这个异常:

 SEVERE: Servlet.service() for servlet [Faces Servlet] in context with path [/BibliothequeJSFHib1] threw exception [org/hibernate/Session] with root cause
java.lang.ClassNotFoundException: org.hibernate.Session

有人知道如何解决它吗?

您的 FournisseurService - 类尝试打开休眠会话。

public class FournisseurService {
    public List findAllList() { 
        Session session=HibernateUtil.getSessionFactory().getCurrentSession(); 
        ...
    }
}

Hibernate Session是一个ORM-Mapper(Database-Stuff),是Hibernate-Core Library的一部分,必须添加到您的项目中。

最新更新