2012-06-27 45 views
0

Hibernate的4.1根據我剛纔的問題integration of jsf2.0 and spring 3.1 and hibernate 4.1集成JSF 2,春季3.1,並在Eclipse

我想我的代碼有問題。我很困惑。我做了其他建議,但仍然收到錯誤404:說明請求的資源(/jsfspringhiber/default.xhtml)不可用。我不使用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> 

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> 

我的項目的結構:

enter image description here

回答

0

我用maven共享記錄罐子。正確的答案是here

的結構是:

enter image description here

0

讓我們做一兩件事,這裏是集成Spring和JSF的基本骨架的代碼。如果您能夠使用此代碼查看登錄頁面,那麼我們將從此構建解決方案。

用下列文件創建一個新的動態Web項目: 在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> 

的面孔,配置。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. 下載最新的GA Spring框架從here釋放,包括在zip文件中的dist文件夾中的罐子。

  2. here下載JSF罐子,你可以抓住最新的2.1.10。

  3. 包括從here