【问题标题】:Spring 4 + Hibernate 4 + JSF 2.2 + PrimeFaces 4 - NullPointerException when accessing objects attributesSpring 4 + Hibernate 4 + JSF 2.2 + PrimeFaces 4 - 访问对象属性时出现 NullPointerException
【发布时间】:2014-09-02 15:50:56
【问题描述】:

这是我的第一个 Spring 应用程序。它在域中有两个类:Product 和 ProductType。

我通过@Controller注解访问托管bean,作用域为@Scope("request")

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:p="http://www.springframework.org/schema/p"
   xmlns:aop="http://www.springframework.org/schema/aop"
   xmlns:tx="http://www.springframework.org/schema/tx"
   xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
   http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
   http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd">

<bean id="propertyConfigurer"
      class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"
      p:location="/WEB-INF/jdbc.properties" />

<bean id="dataSource"
      class="org.springframework.jdbc.datasource.DriverManagerDataSource"
      p:driverClassName="${jdbc.driverClassName}"
      p:url="${jdbc.url}"
      p:username="${jdbc.username}"
      p:password="${jdbc.password}" />

<bean class="org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor" />
<bean id="productRepository"
      class="com.website.repository.ProductRepository" />
<bean id="productTypeRepository"
      class="com.website.repository.ProductTypeRepository" />
<bean id="productMB"
      class="com.website.managedbean.ProductMB" />
<bean id="productTypeMB"
      class="com.website.managedbean.ProductTypeMB" />
<bean id="product"
      class="com.website.model.Product" />
<bean id="productType"
      class="com.website.model.ProductType" />

<bean id="entityManagerFactory"
      class="org.springframework.orm.jpa.LocalEntityManagerFactoryBean" />
<bean class="org.springframework.orm.jpa.JpaTransactionManager">
    <property name="entityManagerFactory" ref="entityManagerFactory" />
</bean>
<tx:annotation-driven />

jdbc.properties 文件:

# jdbc.X
jdbc.driverClassName=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/products
jdbc.username=root
jdbc.password=root

# hibernate.X
hibernate.dialect=org.hibernate.dialect.MySQL5Dialect
hibernate.show_sql=true
hibernate.hbm2ddl.auto=validate

我的 faces-config.xml 只有:

<application>
<el-resolver>org.springframework.web.jsf.el.SpringBeanFacesELResolver</el-resolver>
</application>

在 web.xml 中我添加了两个监听器:ContextLoaderListener 和 RequestContextListener。加上 DispatcherServlet。

ProductRepository 用@Repository 注释并使用@PersistenceContext

ProductMB 使用 @Autowired 注入 ProductRepository。

index.xhtml:

<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:h="http://xmlns.jcp.org/jsf/html"
      xmlns:f="http://xmlns.jcp.org/jsf/core"
      xmlns:p="http://primefaces.org/ui">
    <h:head>
        <title>Product</title>
    </h:head>
    <h:body>
        <h:form>
            <p:growl id="messages"
                     globalOnly="true"
                     autoUpdate="true"
                     sticky="true" />
            <p:panel header="New Product">
                <h:panelGrid columns="3">
                    <p:outputLabel for="product-name"
                                   value="Name:" />
                    <p:inputText id="product-name"
                                 value="#{productMB.product.name}" />
                    <p:message for="product-name" />

                    <p:commandButton value="Insert"
                                     action="#{productMB.save}"
                                     ajax="false" />
                </h:panelGrid>
            </p:panel>
        </h:form>
    </h:body>
</html>

P.S.:使用 Netbeans 8.0 创建项目

【问题讨论】:

    标签: spring hibernate jsf primefaces nullpointerexception


    【解决方案1】:

    这是一个完全配置的工作项目 JSF2/primefaces/Spring4/Hibernate4

    【讨论】:

    • 哇,谢谢!稍后我会看看并与我所做的比较。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-05-22
    • 1970-01-01
    • 1970-01-01
    • 2012-11-12
    • 2012-02-16
    • 1970-01-01
    • 2015-06-10
    相关资源
    最近更新 更多