La integración de JSF 2, Spring 3.1 y Hibernate 4.1 en Eclipse

De acuerdo a mi pregunta anterior la integración de jsf2.0 y la primavera 3.1 y 4.1 hibernate

Creo que mi código tiene un problema. Estoy realmente confundido. Hice como otros advertido, pero sigue recibiendo el error 404:Descripción Del recurso solicitado (/jsfspringhiber/default.xhtml) no está disponible. Yo no uso de maven.

Customer.java

package main;
public class Customer{ 
    public long customerId;
    public String name;
    public String address;
    public String createdDate;
    public long getCustomerId() {
        return customerId;
    }
    public void setCustomerId(long customerId) {
        this.customerId = customerId;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getAddress() {
        return address;
    }
    public void setAddress(String address) {
        this.address = address;
    }
    public String getCreatedDate() {
        return createdDate;
    }
    public void setCreatedDate(String createdDate) {
        this.createdDate = createdDate;
    }
}

CustomerBean.java

package main;
import java.io.Serializable;
import java.util.List;

import main.CustomerBo;
import main.Customer;

public class CustomerBean implements Serializable{

    CustomerBo customerBo;
    public String name;
    public String address;
    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    public void setCustomerBo(CustomerBo customerBo) {
        this.customerBo = customerBo;
    }

    public CustomerBo getCustomerBo() {
        return customerBo;
    }

    //get all customer data from database
    public List<Customer> getCustomerList(){
        return customerBo.findAllCustomer();
    }

    //add a new customer data into database
    public String addCustomer(){

        Customer cust = new Customer();
        cust.setName(getName());
        cust.setAddress(getAddress());

        customerBo.addCustomer(cust);

        clearForm();

        return "";
    }

    //clear form values
    private void clearForm(){
        setName("");
        setAddress("");
    }

}

CustomerBo.java

import java.util.List;

import main.Customer;

public interface CustomerBo{

    void addCustomer(Customer customer);

    List<Customer> findAllCustomer();

}

CustomerBoImpl.java

public class CustomerBoImpl implements CustomerBo{

    CustomerDao customerDao;

    public void setCustomerDao(CustomerDao customerDao) {
        this.customerDao = customerDao;
    }

    public void addCustomer(Customer customer){

        customerDao.addCustomer(customer);

    }

    public List<Customer> findAllCustomer(){

        return customerDao.findAllCustomer();
    }
}

CustomerDao.java

public interface CustomerDao{

    void addCustomer(Customer customer);

    List<Customer> findAllCustomer();

}

CustomerDaoImpl.java

public class CustomerDaoImpl implements CustomerDao{
    private SessionFactory sessionFactory;
    public SessionFactory getSessionFactory() {
        return sessionFactory;}
    public void setSessionFactory(SessionFactory sessionFactory) {
         this.sessionFactory = sessionFactory;
    }

    public void addCustomer(Customer customer){


        getSessionFactory().getCurrentSession().save

(customer);

    }

    public List<Customer> findAllCustomer(){

        List list = getSessionFactory().getCurrentSession

().createQuery("from Customer").list();
        return list;

    }
}

Customer.hbm.xml

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!-- Generated Jun 27, 2012 1:01:10 PM by Hibernate Tools 3.4.0.CR1 -->
<hibernate-mapping>
    <class name="main.Customer" table="CUSTOMER">
        <id name="customerId" type="long">
            <column name="CUSTOMER_ID" />
            <generator class="assigned" />
        </id>
        <property name="name" type="java.lang.String">
            <column name="NAME" />
        </property>
        <property name="address" type="java.lang.String">
            <column name="ADDRESS" />
        </property>
        <property name="createdDate" type="java.lang.String">
            <column name="CREATED_DATE" />
        </property>
    </class>
</hibernate-mapping>

CustomerBean.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.springframework.org/schema/beans 
    http://www.springframework.org/schema/beans/spring-beans-3.1xsd">

    <bean id="customerBo" 
         class="main.CustomerBoImpl" >
        <property name="customerDao" ref="customerDao" />
    </bean>

    <bean id="customerDao" 
         class="main.CustomerDaoImpl" >
        <property name="sessionFactory" ref="sessionFactory" />
    </bean>

</beans>

DataSource.xml

<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.1.xsd">



  <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
    <property name="driverClassName" value="oracle.jdbc.driver.OracleDriver" />
    <property name="url" value="jdbc:oracle:thin:@Mohsen-PC:1521:mydb" />
    <property name="username" value="system" />
    <property name="password" value="123" />
  </bean>

</beans>

HibernateSessionFactory.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.1.xsd">

<!-- Hibernate session factory -->
<bean id="sessionFactory" 
     class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">

    <property name="dataSource">
      <ref bean="dataSource"/>
    </property>

    <property name="hibernateProperties">
       <props>
         <prop key="hibernate.dialect">org.hibernate.dialect.Oracle10gDialect</prop>
         <prop key="hibernate.show_sql">true</prop>
       </props>
    </property>

    <property name="mappingResources">
    <list>
          <value>main/Customer.hbm.xml</value>
    </list>
     </property>    

</bean>
</beans>

applicationContext.xml

<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.1.xsd">

    <!-- Database Configuration -->
    <import resource="main/DataSource.xml"/>
    <import resource="main/HibernateSessionFactory.xml"/>

    <!-- Beans Declaration -->
    <import resource="main/CustomerBean.xml"/>

</beans>

faces-config.xml

<?xml version="1.0" encoding="UTF-8"?>

<faces-config
    xmlns="http://java.sun.com/xml/ns/javaee"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-facesconfig_2_0.xsd"
    version="2.0">
<application>
        <el-resolver>
            org.springframework.web.jsf.el.SpringBeanFacesELResolver
        </el-resolver>
    </application>

    <managed-bean>
        <managed-bean-name>customer</managed-bean-name>
        <managed-bean-class>main.CustomerBean</managed-bean-class>
        <managed-bean-scope>session</managed-bean-scope>
        <managed-property>
            <property-name>customerBo</property-name>
            <value>#{customerBo}</value>
        </managed-property>
    </managed-bean>
</faces-config>

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" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" version="3.0">
  <display-name>jsfspringhiber</display-name>
   <!-- Add Support for Spring -->
  <listener>
    <listener-class>
        org.springframework.web.context.ContextLoaderListener
    </listener-class>
  </listener>
  <listener>
    <listener-class>
        org.springframework.web.context.request.RequestContextListener
    </listener-class>
  </listener>
   <!-- Change to "Production" when you are ready to deploy -->
  <context-param>
    <param-name>javax.faces.PROJECT_STAGE</param-name>
    <param-value>Development</param-value>
  </context-param>

  <!-- Welcome page -->
  <welcome-file-list>
  <welcome-file>default.xhtml</welcome-file>
</welcome-file-list>

    <!-- JSF mapping -->
  <servlet>
    <servlet-name>Faces Servlet</servlet-name>
    <servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
    <load-on-startup>1</load-on-startup>
  </servlet>
   <!-- Map these files with JSF -->
  <servlet-mapping>
  <servlet-name>Faces Servlet</servlet-name>
  <url-pattern>*.xhtml</url-pattern>
</servlet-mapping>
  <context-param>
    <description>State saving method: 'client' or 'server' (=default). See JSF Specification 2.5.2</description>
    <param-name>javax.faces.STATE_SAVING_METHOD</param-name>
    <param-value>client</param-value>
  </context-param>
  <context-param>
    <param-name>javax.servlet.jsp.jstl.fmt.localizationContext</param-name>
    <param-value>resources.application</param-value>
  </context-param>
  <listener>
    <listener-class>com.sun.faces.config.ConfigureListener</listener-class>
  </listener>
</web-app>

predeterminado.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"
      >
    <h:head>
        <h:outputStylesheet library="css" name="table-style.css"  />
    </h:head>

    <h:body>

        <h1>JSF 2.0 + Spring + Hibernate Example</h1>

        <h:dataTable value="#{customer.getCustomerList()}" var="c"
                styleClass="order-table"
                headerClass="order-table-header"
                rowClasses="order-table-odd-row,order-table-even-row"
            >

            <h:column>
                <f:facet name="header">
                    Customer ID
                </f:facet>
                    #{c.customerId}
            </h:column>

            <h:column>
                <f:facet name="header">
                    Name
                </f:facet>
                    #{c.name}
            </h:column>

            <h:column>
                <f:facet name="header">
                    Address
                </f:facet>
                    #{c.address}
            </h:column>

            <h:column>
                <f:facet name="header">
                    Created Date
                </f:facet>
                    #{c.createdDate}
            </h:column>

        </h:dataTable>

        <h2>Add New Customer</h2>
        <h:form>

            <h:panelGrid columns="3">

                Name : 
                <h:inputText id="name" value="#{customer.name}" 
                    size="20" required="true"
                    label="Name" >
                </h:inputText>

                <h:message for="name" style="color:red" />

                Address : 
                <h:inputTextarea id="address" value="#{customer.address}" 
                    cols="30" rows="10" required="true"
                    label="Address" >
                </h:inputTextarea>

                <h:message for="address" style="color:red" />

            </h:panelGrid>

            <h:commandButton value="Submit" action="#{customer.addCustomer()}" />

        </h:form>

    </h:body>

</html>

la estructura de mi proyecto:

La integración de JSF 2, Spring 3.1 y Hibernate 4.1 en Eclipse

InformationsquelleAutor samira | 2012-06-27

2 Kommentare

  1. 0

    Permite hacer una cosa, Aquí está el código para un esquema básico para la integración de Spring y JSF. Si usted es capaz de ver la página de inicio de sesión con este código, a continuación, vamos a construir la solución de este.

    Crear un nuevo Proyecto Web Dinámico con los siguientes archivos:
    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" 
         xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" 
         xsi:schemaLocation="http://java.sun.com/xml/ns/javaee    
         http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID"  
         version="3.0">
    
    <welcome-file-list>
        <welcome-file>index.xhtml</welcome-file>
    </welcome-file-list>
    <servlet>
        <servlet-name>Faces Servlet</servlet-name>
        <servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>Faces Servlet</servlet-name>
        <url-pattern>*.xhtml</url-pattern>
    </servlet-mapping>
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/applicationContext*.xml</param-value>
    </context-param>
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
    <listener>
        <listener-class>org.springframework.web.context.request.RequestContextListener</listener-class>
    </listener>
    </web-app>

    faces-config.xml

    <?xml version="1.0" encoding="UTF-8"?>
    
    <faces-config
        xmlns="http://java.sun.com/xml/ns/javaee"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
        http://java.sun.com/xml/ns/javaee/web-facesconfig_2_0.xsd"
        version="2.0">
    
        <application>
        <el-resolver>org.springframework.web.jsf.el.SpringBeanFacesELResolver</el-resolver>
        </application>
    
    </faces-config>

    applicationContext.xml

    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans.xsd
       http://www.springframework.org/schema/context
       http://www.springframework.org/schema/context/spring-context.xsd">
    
       <context:annotation-config/>
       <context:component-scan base-package="com.examples" />
    
    </beans>

    Páginas de prueba: índice.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">
    <h:head>
      <title>Welcome</title>
    </h:head>
    <h:body>
      <h:form>
         <h3>Please enter your name and password.</h3>   
         <table>
            <tr>
               <td>Name:</td>
               <td><h:inputText value="#{userBean.name}"/></td>
            </tr>
            <tr>
               <td>Password:</td>
               <td><h:inputSecret value="#{userBean.password}"/></td>
            </tr>
         </table>
         <p><h:commandButton value="Login" action="welcome"/></p>
      </h:form>
    </h:body>
    </html>

    de bienvenida.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">
    <h:head>
      <title>Welcome</title>
    </h:head>
    <h:body>
      <h3>Spring integration with JavaServer Faces Successful, #{userBean.name}!</h3>
    </h:body>
    </html>

    Y, finalmente, con el respaldo de la haba: UserBean.java

    package com.examples;
    
    import java.io.Serializable;
    
    import org.springframework.context.annotation.Scope;
    import org.springframework.stereotype.Component;
    
    @Component
    @Scope("request")
    public class UserBean implements Serializable {
    private static final long serialVersionUID = 1L;
    private String name;
    private String password;
    
    public String getName() { return name; }   
    public void setName(String newValue) { name = newValue; }
    
    public String getPassword() { return password; }
    public void setPassword(String newValue) { password = newValue; }   
    }

    Que es, nada más. Sólo tienes que copiar y pegar el código en un proyecto de ejemplo y vamos a ver qué podemos hacer después. Una cosa más que he notado es que la carpeta lib en WEB-INF parece vacía. Usted necesitará la siguiente frascos para ejecutar este proyecto:

    1. Descargar la Última GA de liberación de Primavera de Marco de aquí e incluyen los frascos en el dist carpeta del archivo zip.

    2. Descarga JSF frascos de aquí, usted puede obtener las últimas 2.1.10.

    3. Incluyen commons-logging frasco de aquí

Kommentieren Sie den Artikel

Bitte geben Sie Ihren Kommentar ein!
Bitte geben Sie hier Ihren Namen ein

Pruebas en línea