PrimeFaces + Spring Data + MongoDB Integration

Amr Mohammed
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.
Address.java
package net.javabeat.springdata.jpa.data;
import java.math.BigInteger;
import org.springframework.data.annotation.Id;
import org.springframework.data.mongodb.core.mapping.Document;
@Document
public class Address {
 @Id
 private BigInteger id;
 private String addressCountry;
 private String addressCity;
 public BigInteger getId() {
 return id;
 }
 public void setId(BigInteger id) {
 this.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;
 }
}
Employee.java
package net.javabeat.springdata.jpa.data;
import java.math.BigInteger;
import org.springframework.data.annotation.Id;
import org.springframework.data.mongodb.core.mapping.Document;
@Document(collection="employee")
public class Employee {
 @Id
 private BigInteger id;
 private String employeeName;
 private Address address = new Address();
 public BigInteger getId() {
 return id;
 }
 public void setId(BigInteger id) {
 this.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.
SpringContext.xml

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

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.
EmployeeRepository.java
package net.javabeat.springdata.repo;
import java.math.BigInteger;
import net.javabeat.springdata.jpa.data.Employee;
import org.springframework.data.repository.CrudRepository;
import org.springframework.stereotype.Repository;
@Repository
public interface EmployeeRepository extends CrudRepository<Employee, BigInteger>{}

4. Spring Bean Service

RegistrationService.java
package net.javabeat.springdata.beans;
import net.javabeat.springdata.repo.EmployeeRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
@Component
public class RegistrationService {
 @Autowired
 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.
faces-config.xml


 
 net.javabeat.jsf.application
 msg
 
 org.springframework.web.jsf.el.SpringBeanFacesELResolver


6. PrimeFaces Managed Bean

RegistrationManagedBean.java
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;
import net.javabeat.springdata.jpa.data.Employee;
import com.google.common.collect.Lists;
@ManagedBean
@SessionScoped
public class RegistrationManagedBean {
 private Employee employee = new Employee();
 private List<Employee> employees = new ArrayList<Employee>();
 @ManagedProperty(value="#{registrationService}")
 private RegistrationService service;
 public Employee getEmployee() {
 return employee;
 }
 public void setEmployee(Employee employee) {
 this.employee = employee;
 }
 public List<Employee> getEmployees() {
 this.employees = Lists.newArrayList(this.service.getRepository().findAll());
 return employees;
 }
 public void setEmployees(List<Employee> employees) {
 this.employees = employees;
 }
 public RegistrationService getService() {
 return service;
 }
 public void setService(RegistrationService service) {
 this.service = service;
 }
 public String register(){
 this.service.getRepository().save(employee);
 this.employee = new Employee();
 return "";
 }
}

7. Web Deployment Descriptor

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

 
 State saving method: 'client' or 'server'
 (=default). See JSF Specification 2.5.2
 
 javax.faces.STATE_SAVING_METHOD
 server
 
 
 javax.faces.application.CONFIG_FILES
 /WEB-INF/faces-config.xml
 
 
 contextConfigLocation
 /WEB-INF/spring-config/*.xml
 
 
 Faces Servlet
 javax.faces.webapp.FacesServlet
 1
 
 
 Faces Servlet
 /faces/*
 
 
 Faces Servlet
 *.xhtml
 
 
 org.springframework.web.context.ContextLoaderListener
 
 
 com.sun.faces.config.ConfigureListener
 

8. The View

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

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

9. Primefaces + Spring Data + MongoDB Demo

9. Database Records

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

Comments