繁体   English   中英

在Eclipse中集成JSF 2,Spring 3.1和Hibernate 4.1

[英]Integration of JSF 2, Spring 3.1 and Hibernate 4.1 in Eclipse

根据我之前的问题, 将jsf2.0与spring 3.1和hibernate 4.1集成在一起

我认为我的代码有问题。 我真的很困惑。 我按照其他建议做,但仍然出现错误404:描述所请求的资源(/jsfspringhiber/default.xhtml)不可用。 我不使用Maven。

客户.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>

数据源.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>

default.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>

我的项目的结构:

在此处输入图片说明

让我们做一件事,这是用于集成Spring和JSF的基本框架的代码。 如果您能够看到带有此代码的登录页面,那么我们将以此为基础构建解决方案。

使用以下文件创建一个新的Dynamic Web Project: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>

测试页:index.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>

welcome.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>

最后是后备bean: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; }   
}

就是这样,仅此而已。 只需将这段代码复制并粘贴到示例项目中,然后看看我们以后可以做什么。 我注意到的另一件事是WEB-INF下的lib文件夹似乎为空。 您将需要以下jar运行该项目:

  1. 此处下载Spring Framework的最新GA版本,并将jar文件包含在zip文件的dist文件夹中。

  2. 这里下载JSF jars,可以获取最新的2.1.10。

  3. 这里包括公共记录罐

我用Maven。 正确的答案在这里

结构是:

在此处输入图片说明

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM