PrimeFaces + Spring Data + MongoDB Integration

This tutorial guides you through the integration of various frameworks like PrimeFaces, Spring Data and MongoDB. In our previous tutorials, we have explained more about the each topics PrimeFaces, Spring Data & MongoDB. Here a simple example for using the Registration form where the details will be finally stored in the MongoDB database.

1. Java Bean

The entities that will be used in this example are Address and Employee.


import java.math.BigInteger;


public class Address {
	private BigInteger id;
	private String addressCountry;
	private String addressCity;

	public BigInteger getId() {
		return id;
	public void setId(BigInteger id) { = id;
	public String getAddressCountry() {
		return addressCountry;
	public void setAddressCountry(String addressCountry) {
		this.addressCountry = addressCountry;
	public String getAddressCity() {
		return addressCity;
	public void setAddressCity(String addressCity) {
		this.addressCity = addressCity;


import java.math.BigInteger;


public class Employee {
	private BigInteger id;
	private String employeeName;
	private Address address = new Address();

	public BigInteger getId() {
		return id;
	public void setId(BigInteger id) { = id;
	public String getEmployeeName() {
		return employeeName;
	public void setEmployeeName(String employeeName) {
		this.employeeName = employeeName;
	public Address getAddress() {
		return address;
	public void setAddress(Address address) {
		this.address = address;

2. Spring Configurations (Configure MongoDB)

In this step, you have to write the spring configuration for configuring the MongoDB database and also define the beans to the spring container. Also we have to add the spring data repositories.


<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns=""
	xmlns:xsi="" xmlns:context=""

    <!-- Spring Beans -->
    <context:component-scan base-package="net.javabeat.springdata.beans.*"></context:component-scan>

	<!-- Register Mongo Instance -->
	<mongo:mongo id="mongo" host="localhost" port="27017" />

	<!--  for defining mongo template -->
	<bean id="mongoTemplate" class="">
		<constructor-arg ref="mongo" />
		<constructor-arg name="databaseName" value="JavaBeat" />

	<!-- For consider the using of annotations foe defining Spring Bean -->
	<context:annotation-config />

	<!-- For defining Spring Bean -->
	<context:component-scan base-package="net.javabeat.springdata.beans" />
	<!-- For defining mongo repository -->
	<mongo:repositories base-package="net.javabeat.springdata.repo" />


3. Spring Data Repository

Spring Data defines abstract repositories which has to be extended by your implementation business domain. Note that, you need not implement any of the methods, spring data itself create an implementation at run time. That is the great advantage of using the spring data for the database applications. If you want to learn this topic, please find list of articles about Spring Data.

package net.javabeat.springdata.repo;

import java.math.BigInteger;

import org.springframework.stereotype.Repository;

public interface EmployeeRepository extends CrudRepository&lt;Employee, BigInteger&gt;{}

4. Spring Bean Service

package net.javabeat.springdata.beans;

import net.javabeat.springdata.repo.EmployeeRepository;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

public class RegistrationService {
	private EmployeeRepository repository;

	public RegistrationService(){


	public EmployeeRepository getRepository() {
		return repository;

	public void setRepository(EmployeeRepository repository) {
		this.repository = repository;

5. PrimeFaces / JSF Configurations

Another configuration we need to run this example is to define the Java Server Faces (JSF) configurations file. We are using the PrimeFaces as the view layer.


<?xml version="1.0" encoding="UTF-8"?>
<faces-config xmlns=""

6. PrimeFaces Managed Bean

package net.javabeat.primefaces.managedbeans;

import java.util.ArrayList;
import java.util.List;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.SessionScoped;

import net.javabeat.springdata.beans.RegistrationService;


public class RegistrationManagedBean {

	private Employee employee = new Employee();

	private List&lt;Employee&gt; employees = new ArrayList&lt;Employee&gt;();

	private RegistrationService service;

	public Employee getEmployee() {
		return employee;

	public void setEmployee(Employee employee) {
		this.employee = employee;

	public List&lt;Employee&gt; getEmployees() {
		this.employees = Lists.newArrayList(this.service.getRepository().findAll());
		return employees;

	public void setEmployees(List&lt;Employee&gt; employees) {
		this.employees = employees;

	public RegistrationService getService() {
		return service;

	public void setService(RegistrationService service) {
		this.service = service;

	public String register(){
		this.employee = new Employee();
		return &quot;&quot;;

7. Web Deployment Descriptor

It is the web deployment descriptor (web.xml) that is used for runing this example web application. It has the

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="" xmlns="" xmlns:web="" xsi:schemaLocation="" id="WebApp_ID" metadata-complete="true" version="2.5">

    <description>State saving method: 'client' or 'server'
						(=default). See JSF Specification 2.5.2



    <servlet-name>Faces Servlet</servlet-name>

    <servlet-name>Faces Servlet</servlet-name>

    <servlet-name>Faces Servlet</servlet-name>


8. The View

Here is the PrimeFaces view that is used in this example.


<html xmlns=""
		<script name="jquery/jquery.js" library="primefaces"></script>
		<h:form prependId="false">
			<h:panelGrid columns="2">
				<h:outputText value="Enter Employee Name:"/>
				<p:inputText value="#{registrationManagedBean.employee.employeeName}"></p:inputText>
				<h:outputText value="Enter Employee Address Country:"/>
				<p:inputText value="#{registrationManagedBean.employee.address.addressCountry}"></p:inputText>
				<h:outputText value="Enter Employee Address City:"/>
				<p:inputText value="#{registrationManagedBean.employee.address.addressCity}"></p:inputText>
			<p:commandButton value="Register" action="#{registrationManagedBean.register}" ajax="false"/>
			<h:panelGrid columns="1">
				<p:dataTable value="#{registrationManagedBean.employees}" var="employee">
					<p:column headerText="Employee's Name">
						<h:outputText value="#{employee.employeeName}"/>
					<p:column headerText="Employee's Country">
						<h:outputText value="#{employee.address.addressCountry}"/>
					<p:column headerText="Employee's City">
						<h:outputText value="#{employee.address.addressCity}"/>

9. Primefaces + Spring Data + MongoDB Demo

Primefaces + Spring Data + MongoDB Example 1

MongoDB + PrimeFaces  RegisterEmployees

9. Database Records

It is the snapshot of the database records after executing the above program.

MongoDB Persisted Records

Leave a Reply

Your email address will not be published. Required fields are marked *

Pin It on Pinterest

Share This

Share this post with your friends!