0

我想用 PrimeFaces、Spring 和 Hibernate 开发我的应用程序,我举了这个例子,但它确实有效。我不知道如何配置 applicationContext.xml、web.xml、faces-config.xml,我不知道我是否错过了 jar 。

我有这个错误:

javax.el.PropertyNotFoundException: /index.xhtml @14,50 value="#{customer.lists}": 在类型 comtic.scrum.managedBean.CustomerBean 上找不到属性“列表”

我使用 tomcat 7 和 JBoss Dev Studio、primeFaces 3.4.1、spring-3.2.0.RC1、hibernate-release-4.0.1.Final

我的表是带有 (customerId, name, address, createdDate) 列的客户

这是 WEB-INF/lib 中的 jar 列表:

  1. antlr-2.7.7.jar
  2. common-annotations.jar
  3. commons-beanutils.jar
  4. commons-collections.jar
  5. commons-collections-3.2.1.jar
  6. commons-digester.jar
  7. commons-logging.jar
  8. dom4j-1.6.1.jar
  9. hibernate-commons-annotations-4.0.1.Final.jar
  10. hibernate-core-4.0.1.Final.jar
  11. hibernate-jpa-2.0-api-1.0.1.Final.jar
  12. javassist-3.15.0-GA.jar
  13. jboss-logging-3.1.0.CR2.jar
  14. jboss-transaction-api_1.1_spec-1.0.0.Final.jar
  15. jsf-api-2.1.6.jar
  16. jsf-impl-2.1.6.jar
  17. jslt.jar
  18. mysql-connector-java-5.0.8-bin.jar
  19. primefaces-3.4.1.jar
  20. spring-aop-3.2.0.RC1.jar
  21. spring-aspects-3.2.0.RC1.jar
  22. spring-beans-3.2.0.RC1.jar
  23. spring-context-3.2.0.RC1.jar
  24. spring-context-support-3.2.0.RC1.jar
  25. spring-core-3.2.0.RC1.jar
  26. spring-expression-3.2.0.RC1.jar
  27. spring-instrument-3.2.0.RC1.jar
  28. spring-instrument-tomcat-3.2.0.RC1.jar
  29. spring-jdbc-3.2.0.RC1.jar
  30. spring-jms-3.2.0.RC1.jar
  31. spring-orm-3.2.0.RC1.jar
  32. spring-oxm-3.2.0.RC1.jar
  33. spring-struts-3.2.0.RC1.jar
  34. spring-test-3.2.0.RC1.jar
  35. spring-tx-3.2.0.RC1.jar
  36. spring-web-3.2.0.RC1.jar
  37. spring-webmvc-3.2.0.RC1.jar
  38. spring-webmvc-portlet-3.2.0.RC1.jar
  39. 标准.jar

这是 Customer.java

import java.util.Date;

/**
 * Customer generated by hbm2java
 */
public class Customer implements java.io.Serializable {

    private Integer customerId;
    private String name;
    private String address;
    private Date createdDate;

    public Customer() {
    }

    public Customer(String name, String address, Date createdDate) {
        this.name = name;
        this.address = address;
        this.createdDate = createdDate;
    }

    public Integer getCustomerId() {
        return this.customerId;
    }

    public void setCustomerId(Integer customerId) {
        this.customerId = customerId;
    }

    public String getName() {
        return this.name;
    }

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

    public String getAddress() {
        return this.address;
    }

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

    public Date getCreatedDate() {
        return this.createdDate;
    }

    public void setCreatedDate(Date createdDate) {
        this.createdDate = createdDate;
    }

}

客户DAO.java:

import java.util.List;

import comtic.scrum.customer.model.Customer;

public interface CustomerDao{

    void addCustomer(Customer customer);

    List<Customer> findAllCustomer();

}

CustomerDaoImp.java

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

import comtic.scrum.customer.dao.CustomerDao;
import comtic.scrum.customer.model.Customer;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

public class CustomerDaoImpl extends 
       HibernateDaoSupport implements CustomerDao{

    public void addCustomer(Customer customer){

        customer.setCreatedDate(new Date());
        getHibernateTemplate().save(customer);

    }

    public List<Customer> findAllCustomer(){

        return getHibernateTemplate().find("from Customer");

    }
}

客户服务.java

import java.util.List;

import comtic.scrum.customer.model.Customer;

public interface CustomerService {
    void addCustomer(Customer customer);

    List<Customer> findAllCustomer();

}

CustomerServiceImp.java

import java.util.List;

import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import comtic.scrum.customer.dao.CustomerDao;
import comtic.scrum.customer.model.Customer;
import comtic.scrum.customer.service.CustomerService;

public class CustomerServiceImp extends 
HibernateDaoSupport  implements CustomerService {

    CustomerDao customerDao;

    public CustomerDao getCustomerDao() {
        return customerDao;
    }

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

    public void addCustomer(Customer customer){

        customerDao.addCustomer(customer);

    }

    public List<Customer> findAllCustomer(){

        return customerDao.findAllCustomer();
    }

}

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


    <bean id="customerService" 
         class="comtic.scrum.customer.service.imp.CustomerServiceImp" >
        <property name="customerDao" ref="customerDao" />
    </bean>

    <bean id="customerDao" 
         class="comtic.scrum.customer.dao.imp.CustomerDaoImp" >
        <property name="sessionFactory" ref="sessionFactory">
        </property>

    </bean>

</beans>

CustomerBean.java

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.List;

import comtic.scrum.customer.model.Customer;
import comtic.scrum.customer.service.CustomerService;

public class CustomerBean implements Serializable {
    //DI via Spring
        CustomerService customerService;

        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;
        }



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

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

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

            customerService.addCustomer(cust);

            clearForm();

            return "";
        }

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

        public comtic.scrum.customer.service.CustomerService getCustomerService() {
        return customerService;
    }

        public void setCustomerService(
            comtic.scrum.customer.service.CustomerService customerService) {
        this.customerService = customerService;
    }


}

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

<bean 
   class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
   <property name="location">
     <value>WEB-INF/classes/config/database/db.properties</value>
   </property>
</bean>

  <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
    <property name="driverClassName" value="${jdbc.driverClassName}" />
    <property name="url" value="${jdbc.url}" />
    <property name="username" value="${jdbc.username}" />
    <property name="password" value="${jdbc.password}" />
  </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.xsd">


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

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

    <property name="mappingResources">
    <list>
          <value>comtic/scrum/customer/hibernate/Customer.hbm.xml</value>
    </list>
     </property>

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

</bean>
</beans>

面孔-config.xml

<?xml version="1.0" encoding="UTF-8"?>
<faces-config version="2.0" xmlns="http://java.sun.com/xml/ns/javaee"
 xmlns:xi="http://www.w3.org/2001/XInclude"
 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">

 <managed-bean>
  <managed-bean-name>customer</managed-bean-name>
  <managed-bean-class>comtic.scrum.managedBean.CustomerBean</managed-bean-class>
  <managed-bean-scope>session</managed-bean-scope>
  <managed-property>
   <property-name>customerService</property-name>
   <property-class>comtic.scrum.customer.service.CustomerService</property-class>
   <value/>
  </managed-property>
 </managed-bean>

 <application>
  <resource-bundle>
   <base-name>resources</base-name>
   <var>msgs</var>
  </resource-bundle>
 </application>
</faces-config>

web.xml

<?xml version="1.0"?>
<web-app version="3.0" 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-app_3_0.xsd">
 <display-name>Template</display-name>
 <!-- Spring Context Configuration' s Path definition -->
 <context-param>
  <param-name>contextConfigLocation</param-name>
  <param-value>/WEB-INF/applicationContext.xml</param-value>
 </context-param>
 <!-- Project Stage Level -->
 <context-param>
  <param-name>javax.faces.PROJECT_STAGE</param-name>
  <param-value>Development</param-value>
 </context-param>
 <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>*.jsf</url-pattern>
 </servlet-mapping>
 <servlet-mapping>
  <servlet-name>Faces Servlet</servlet-name>
  <url-pattern>*.faces</url-pattern>
 </servlet-mapping>
 <servlet-mapping>
  <servlet-name>Faces Servlet</servlet-name>
  <url-pattern>/faces/*</url-pattern>
 </servlet-mapping>
 <welcome-file-list>
  <welcome-file>index.xhtml</welcome-file>
 </welcome-file-list>
</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"      
      xmlns:pf="http://primefaces.org/ui">

<h:head>

</h:head>
<h:body>
    <h1>PrimeFaces + Spring + Hibernate Example</h1>
    <pf:dataTable value="#{customer.lists}" var="c">
        <h:column>
            <h:outputText value="customerId"/>
        </h:column>
        <h:column>
            <h:outputText value="#{c.name}"/>
        </h:column>
        <h:column>
            <h:outputText value="#{c.address}"/>
        </h:column>
        <h:column>
            <h:outputText value="#{c.createdDate}"/>
        </h:column>     
    </pf:dataTable>

    <h2>Add New Customer</h2>
    <h:form>
        <pf:panelGrid columns="3" >
            Name :              
                    <pf:inputText id="name" label="Name" required="true" size="20" value="#{customer.name}">                        
                    </pf:inputText>                 
                <h:message for="name" style="color:red" />

                Address :               
                    <pf:inputTextarea cols="30" id="address" label="Address" required="true" rows="10" value="#{customer.address}">                     
                    </pf:inputTextarea>
                <h:message for="address" style="color:red" />               
            <pf:button outcome="#{customer.addCustomer}" value="Submit">

            </pf:button>
        </pf:panelGrid>
    </h:form>   
</h:body>
</html>

谢谢

4

1 回答 1

1

是否真的需要所有 Spring/Hibernate/XML 代码来证明问题?当只有一个 XHTML 文件和一个托管 bean 类而没有所有 Spring/Hibernate/XML 噪音时,您会遇到完全相同的问题。学会将问题分成 1 或 2 页。你的问题将近10页长......

无论如何,你得到的例外

javax.el.PropertyNotFoundException: /index.xhtml @14,50 value="#{customer.lists}": 在类型 comtic.scrum.managedBean.CustomerBean 上找不到属性“列表”

只是想告诉您CustomerBean该类缺少 getter 方法getLists()。添加它并确保它返回一个List<Customer>.

public List<Customer> getLists() {
    return lists;
}

与具体问题无关s,属性名称中的复数顺便说一句很奇怪。它只返回一个列表,对吗?如果我是你,我会#{customer.list}getList()吸气剂将其重命名为。

于 2012-11-14T17:43:24.690 回答