Over a million developers have joined DZone.

JSF (PrimeFaces) and Spring Data MongoDB Integration (Part 3)

· Database Zone

Learn NoSQL for free with hands-on sample code, example queries, tutorials, and more.  Brought to you in partnership with Couchbase.

 pom.xml of the project

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
  <modelversion>4.0.0</modelVersion>
  <groupid>com.borislam</groupId>
  <artifactid>mongoShop</artifactId>
  <packaging>war</packaging>
  <version>1.0-SNAPSHOT</version>
  <name>MongoShop Webapp</name>
  <url>http://maven.apache.org</url>
  <dependencies>
  <dependency>
     <groupid>org.jboss.el</groupId>
     <artifactid>com.springsource.org.jboss.el</artifactId>
     <version>2.0.0.GA</version>
 </dependency>
    <dependency> 
     <groupid>org.primefaces.themes</groupId> 
     <artifactid>all-themes</artifactId> 
     <version>1.0.9</version> 
    </dependency>
    <dependency> 
  <groupid>org.primefaces</groupId> 
  <artifactid>primefaces</artifactId> 
  <version>3.4.2</version> 
 </dependency>
 <dependency>
  <groupid>commons-beanutils</groupId>
  <artifactid>commons-beanutils</artifactId>
  <version>1.8.3</version>
 </dependency>
  
 <dependency>
  <groupid>commons-codec</groupId>
  <artifactid>commons-codec</artifactId>
  <version>1.3</version>
 </dependency>
 
 <dependency>
  <groupid>org.apache.directory.studio</groupId>
  <artifactid>org.apache.commons.lang</artifactId>
  <version>2.6</version>
 </dependency>
 
 <dependency>
  <groupid>commons-digester</groupId>
  <artifactid>commons-digester</artifactId>
  <version>1.8</version>
 </dependency>
 
 <dependency>
  <groupid>commons-collections</groupId>
  <artifactid>commons-collections</artifactId>
  <version>3.2</version>
 </dependency>
  
 <dependency>
  <groupid>org.apache.myfaces.core</groupId>
  <artifactid>myfaces-api</artifactId>
  <version>2.1.9</version>
 </dependency>
  
 <dependency>
  <groupid>org.apache.myfaces.core</groupId>
  <artifactid>myfaces-impl</artifactId>
  <version>2.1.9</version>
 </dependency>
  
   <dependency>
  <groupid>org.mongodb</groupId>
  <artifactid>mongo-java-driver</artifactId>
  <version>2.10.1</version>
 </dependency>
    <dependency>
      <groupid>junit</groupId>
      <artifactid>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>
 <dependency>
  <groupid>org.springframework.data</groupId>
  <artifactid>spring-data-mongodb</artifactId>
  <version>1.0.3.RELEASE</version>
 </dependency>
 <dependency>
   <groupid>org.springframework</groupId>
   <artifactid>spring-context</artifactId>
   <version>3.2.0.RELEASE</version>
 </dependency>
 <dependency>
   <groupid>org.springframework</groupId>
   <artifactid>spring-web</artifactId>
   <version>3.2.0.RELEASE</version>
 </dependency>
  </dependencies>
  <repositories>
    <repository>
  <id>java.net</id>
  <url>https://maven.java.net/content/repositories/public/</url>
    </repository>
 <repository> 
  <id>prime-repo</id> 
  <name>PrimeFaces Maven Repository</name> 
  <url>http://repository.primefaces.org</url> 
  <layout>default</layout> 
 </repository>
 
 <repository>
  <id>com.springsource.repository.bundles.release</id>
  <name>SpringSource Enterprise Bundle Repository - SpringSource Releases</name>
  <url>//repository.springsource.com/maven/bundles/release</url>
 </repository>
 
 <repository>
  <id>com.springsource.repository.bundles.external</id>
  <name>SpringSource Enterprise Bundle Repository - External Releases</name>
  <url>//repository.springsource.com/maven/bundles/external</url>
 </repository>
 <repository>
         <releases>
           <enabled>false</enabled>
         </releases>
         <snapshots>
           <enabled>true</enabled>
         </snapshots>
         <id>apache.snapshots</id>
         <name>Apache Snapshot Repository</name>
         <url>https://repository.apache.org/content/repositories/snapshots</url>
    </repository>
 <repository>
          <id>jboss-deprecated-repository</id>
          <name>JBoss Deprecated Maven Repository</name>
          <url>https://repository.jboss.org/nexus/content/repositories/deprecated/</url>
          <layout>default</layout>
          <releases>
            <enabled>true</enabled>
            <updatepolicy>never</updatePolicy>
          </releases>
          <snapshots>
            <enabled>false</enabled>
            <updatepolicy>never</updatePolicy>
          </snapshots>
        </repository>
  </repositories>
  <build>
    <finalname>mongoShop</finalName>
  </build>
</project>

MyFaces
MyFaces is used as the JSF implementation in this application. The following details should be added in web.xml

PrimeFaces Theme
As said before, PrimeFaces library is used to enhance the UI. There is nearly no configuration required for this library. PrimeFaces provides many pre-designed theme for your web application. In our case, we use “blue-sky” theme. We just add the following setting in web.xml

<context-param>
  <param-name>primefaces.THEME</param-name><param-value>glass-x</param-value></context-param>

JSF and Spring Integration:
To integrate JSF with Spring, you have to specify the SpringBeanFacesELResolver in Faces-config.xml
Faces-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">
   <application>
     <el-resolver>org.springframework.web.jsf.el.SpringBeanFacesELResolver</el-resolver>
   </application>
   <factory>
 <partial-view-context-factory>org.primefaces.context.PrimePartialViewContextFactory</partial-view-context-factory>
   </factory>
</faces-config>

Full web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_ID" 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">
 <context-param>
 <param-name>contextConfigLocation</param-name><param-value>WEB-INF/spring-application-context.xml</param-value></context-param>
 <context-param>
  <param-name>errorPageUrl</param-name><param-value>/pages/systemError.do</param-value></context-param>
 <context-param>
  <param-name>facelets.DEVELOPMENT</param-name><param-value>false</param-value></context-param>
 <context-param>
   <param-name>facelets.REFRESH_PERIOD</param-name><param-value>2</param-value></context-param>
 <context-param>
   <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>
 <context-param>
   <param-name>org.apache.myfaces.ALLOW_JAVASCRIPT</param-name><param-value>true</param-value></context-param>
  <context-param>
    <param-name>org.apache.myfaces.AUTO_SCROLL</param-name><param-value>false</param-value></context-param>
 <context-param>
    <param-name>org.apache.myfaces.DETECT_JAVASCRIPT</param-name><param-value>false</param-value></context-param>
 <context-param>
  <param-name>org.apache.myfaces.ERROR_HANDLING</param-name><param-value>false</param-value></context-param>
 <context-param>
  <param-name>org.apache.myfaces.EXPRESSION_FACTORY</param-name><param-value>org.jboss.el.ExpressionFactoryImpl</param-value></context-param>
 <context-param>
  <param-name>org.apache.myfaces.PRETTY_HTML</param-name><param-value>false</param-value></context-param>
 <context-param>
  <param-name>primefaces.THEME</param-name><param-value>glass-x</param-value></context-param> 
 <servlet>
   <servlet-name>Faces Servlet</servlet-name>
   <servlet-class>org.apache.myfaces.webapp.MyFacesServlet</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>
 <listener>
 <listener-class>org.apache.myfaces.webapp.StartupServletContextListener</listener-class>
 </listener>
 <listener>
 <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
 </listener>
</web-app>

MongoDB connection Details
In to order connect to MongoDB, you have to register a MongoDbFactory instance in XML. The connection details is specified in spring-application-context.xml

spring-application-context.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xmlns:util="http://www.springframework.org/schema/util"
 xmlns:mongo="http://www.springframework.org/schema/data/mongo"
 xsi:schemaLocation="http://www.springframework.org/schema/beans       
    http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
          http://www.springframework.org/schema/data/mongo
          http://www.springframework.org/schema/data/mongo/spring-mongo.xsd           
              http://www.springframework.org/schema/data/repository
    http://www.springframework.org/schema/data/repository/spring-repository.xsd
             http://www.springframework.org/schema/context
                http://www.springframework.org/schema/context/spring-context-3.2.xsd
                http://www.springframework.org/schema/util
                http://www.springframework.org/schema/util/spring-util-3.2.xsd">
  
 <context:annotation-config/>
 <context:component-scan base-package="com.borislam"/>
 
 <mongo:mongo host="localhost" port="27017">
  <mongo:options
       connections-per-host="5"      
       connect-timeout="30000"
       max-wait-time="10000"      
       write-number="1"
       write-timeout="0"
       write-fsync="true"/>
 </mongo:mongo>
 
 <mongo:db-factory dbname="test" mongo-ref="mongo"/>
 
 <mongo:repositories base-package="com.borislam.repository" />
   
 <bean id="mongoTemplate" class="org.springframework.data.mongodb.core.MongoTemplate">
     <constructor-arg ref="mongo"/><constructor-arg name="databaseName" value="test"/></bean>
</beans>




The Getting Started with NoSQL Guide will get you hands-on with NoSQL in minutes with no coding needed. Brought to you in partnership with Couchbase.

Topics:

Published at DZone with permission of Boris Lam, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

The best of DZone straight to your inbox.

SEE AN EXAMPLE
Please provide a valid email address.

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.
Subscribe

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}