由于缺少依赖项,无法创建托管Bean

时间:2015-10-02 16:53:33

标签: java eclipse hibernate jsf primefaces

我的Primefaces项目出了问题。我正在使用Primefaces 5.2和Hibernate 5.0 for ORM。我正在使用Eclipse并使用Microsoft SQL Server。我不使用Maven(我注意到有几个人使用过)。我还检查了几个SO问题,没有任何积极的结果,包括:

unable to create managed bean primefaces

Unable to create managed bean UserBean - JSF

错误之前我能够从Java控制器写入我的数据库而没有遇到任何问题,并直接在我的数据库上验证了正确的插入。

我现在正在研究一个LogIn验证器,我正在研究这个类:

package modelo;

import java.util.List; 
import java.util.Date;
import java.util.Iterator; 

import org.hibernate.HibernateException; 
import org.hibernate.Session; 
import org.hibernate.Transaction;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;

import modelo.Empleado;

public class LoginController {

    private static SessionFactory factory;
    private String usuario;
    private String contraseña;

    public String validar(){
        try{
             factory = new Configuration().configure().buildSessionFactory();
             System.out.println("Validando");
        }
        catch (Throwable ex) { 
             System.err.println("Failed to create sessionFactory object." + ex);
             System.out.println("NOPE");
             throw new ExceptionInInitializerError(ex); 
        }

        LoginController LC = new LoginController();

        /*Verificar exista un usuario con ese login_name y contraseña*/

        Session session = factory.openSession();
        Transaction tx = null;      
        try{

            tx = session.beginTransaction();
            List validados = session.createQuery("FROM EMPLEADO WHERE contraseña ='"+contraseña+"' and nombre ='"+usuario+"'").list();

            Empleado emp = (Empleado) validados.get(0);
            if(emp == null){
                System.out.println("Error de Autenticación");
                return "login";
            }
            else{
                System.out.println("Autenticacion Correcta, User: "+usuario+", pass: "+contraseña);
                return "asistencia";
            } 
        }catch(HibernateException e){
            if (tx!=null) tx.rollback();
            e.printStackTrace(); 
        }finally{
            session.close();
        }
        return "login";
    }

    public String getUsuario() {
        return usuario;
    }

    public void setUsuario(String usuario) {
        this.usuario = usuario;
    }

    public String getContraseña() {
        return contraseña;
    }

    public void setContraseña(String contraseña) {
        this.contraseña = contraseña;
    }

}

然而,当我运行它时,我收到以下错误: enter image description here

我已经找到了一种方法来配置"那个"依赖"但没有成功。我可能正确地写了hibernate.cfg.xml,因为我可以像上面提到的那样写入我的数据库,但是这里是该文件的代码:

<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-configuration SYSTEM 
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">

<hibernate-configuration>
  <session-factory>
    <property name="connection.driver_class">com.microsoft.sqlserver.jdbc.SQLServerDriver</property>
    <property name="connection.url">jdbc:sqlserver://localhost:1433;databaseName=Medidas</property>
    <property name="connection.username">sa</property>
    <property name="connection.password">***</property>
    <property name="dialect">org.hibernate.dialect.SQLServerDialect</property>

    <property name="hibernate.connection.pool_size">100</property>        
    <property name="show_sql">false</property>

    <property name="current_session_context_class">thread</property>

    <!-- Mapping files -->
    <mapping resource="Empleado.hbm.xml"/>

  </session-factory>
</hibernate-configuration>

我怀疑它与web.xml文件有关但我没有找到如何配置它。自从我下载基础项目以来,我的web.xml几乎没有变化:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
         xmlns="http://java.sun.com/xml/ns/javaee" 
         xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" 
         version="3.0">
  <!-- The bare minimum needed for JSF 2.2 is a servlet 2.5 or later
       declaration (this uses 3.0) and the mapping for the FacesServlet.
       Setting PROJECT_STAGE to Development is highly recommended
       during initial development so that you get more helpful
       error messages. Whether you want server-side state saving
       (default) or client-side is a more complicated question:
       client-side uses more bandwidth but fewer server resources.
       Client-side also helps to avoid the dreaded view expired exceptions.

       From JSF 2 and PrimeFaces tutorial 
       at http://www.coreservlets.com/JSF-Tutorial/jsf2/
  -->
  <!-- Hibernate Here ??? -->
  <resource-env-ref>
    <resource-env-ref-name>jdbc/DSWebAppDB</resource-env-ref-name>
    <resource-env-ref-type>javax.sql.DataSource</resource-env-ref-type>
  </resource-env-ref>
  <servlet>
    <servlet-name>Faces Servlet</servlet-name>
    <servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
  </servlet>
  <servlet-mapping>
    <servlet-name>Faces Servlet</servlet-name>
    <url-pattern>*.jsf</url-pattern>
  </servlet-mapping>
  <context-param>
    <param-name>javax.faces.PROJECT_STAGE</param-name>
    <param-value>Development</param-value>
  </context-param>
  <context-param>
    <description>State saving method: 'client' or 'server' (default). See JSF Specification section 2.5.2</description>
    <param-name>javax.faces.STATE_SAVING_METHOD</param-name>
    <param-value>client</param-value>
  </context-param>
  <!-- If you go to http://host/project/ (with no file name), it will
       try index.jsf first, welcome.jsf next, and so forth.
   -->
  <welcome-file-list>
    <welcome-file>index.jsf</welcome-file>
    <welcome-file>welcome.jsf</welcome-file>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
</web-app>

有关导致我问题的原因的任何想法?我应该添加我缺少的配置或依赖项吗?提前致谢(如果您需要更多片段或信息,请告诉我)

0 个答案:

没有答案