0

根据我之前的问题集成 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;

    }
}

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

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

应用程序上下文.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>

面孔-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>

默认.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>

我的项目结构:

在此处输入图像描述

4

2 回答 2

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>

面孔-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>

应用程序上下文.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>

欢迎.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 版本,并将 jars 包含在 zip 文件的 dist 文件夹中。

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

  3. 从此处包含公共日志记录 jar

于 2012-06-28T00:17:55.177 回答
0

我使用行家。正确答案在这里

结构是:

在此处输入图像描述

于 2012-07-02T14:34:22.047 回答