Rasshu
Rasshu

Reputation: 1809

Spring 4 + Hibernate 4 + JSF 2.2 + PrimeFaces 4 - NullPointerException when accessing objects attributes

It's my first Spring application. It has two classes in the domain: Product and ProductType.

I'm accessing managed beans through the @Controller annotation, and the scope is @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 />

the jdbc.properties file:

# 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

My faces-config.xml has only:

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

And in the web.xml I added two listeners: ContextLoaderListener and RequestContextListener. Plus the DispatcherServlet.

ProductRepository is annotated with @Repository and uses @PersistenceContext.

ProductMB injects ProductRepository with @Autowired.

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.: Created the project using Netbeans 8.0

Upvotes: 1

Views: 3212

Answers (1)

Armen Arzumanyan
Armen Arzumanyan

Reputation: 2063

Here is a fully configured working project JSF2/primefaces/Spring4/Hibernate4

Upvotes: 1

Related Questions