We have already introduced Spring Boot to Payara Micro, the new fish on the block!, by implementing Spring Boot based RESTful web services and hosted on Payara Micro. While implementing it, we didn’t deal with any of the XML configurations or dependency management needed for the Spring framework with the help of the Spring Boot
With this article, we will take it one step further by introducing new modules from Java EE like JPA, JSF and many others. Spring Boot will ease the development yet again for us with its bootstrapping methodology. After building our sample application containing simple user interface and persistency, we will deploy it onto Payara Micro, the application server that offers running Java EE applications on a cluster with a small footprint (~60mb) and directly from the console.It’s worth to mention that Payara Micro is fully compatible with the Java EE 7 Web Profile - you will see for yourself how easy it is to create a CRUD page that integrates with an embedded database provided by Payara Micro.All of the examples in this article are based on Spring Boot version 1.2.6.RELEASE and Payara Micro version 4.1.153, which are the latest available versions at the time of writing. The complete source code given in the article is available here.
Project Structure and Dependencies
Let’s start our development by taking our Spring Boot REST example as a code basis. The only dependencies that we are going to add are: JSF + PrimeFaces for the user interface and Spring boot starter JPA for the persistency. They are given as follows.
Payara Micro already ships with Mojarra v2.2.11, which is the JSF implementation from Oracle, so providing the PrimeFaces dependency only will suffice on the UI side. Spring Boot’s starter for JPA bundles all the necessary dependencies for Hibernate so we don’t need to provide a persistency provider either. Spring Boot can also auto-configure embedded databases like H2, HSQL or Derby. Since Payara Micro ships with an embedded Derby database already, we don’t even need to write one line of code to connect to that database, other than only extending our repository classes from the class,
JpaRepository. We’ll get to that soon, just stick with us!
Implementing a CRUD pageFirst we should define our domain object as a JPA managed entity. Person class definition that achieves this is given below.
As you can also notice, entity contains bean validation annotations like
Spring Data module provides standardized repository implementation model based on JPA. It maps an interface per managed domain object and the corresponding person repository definition is given below.
For populating the DB, we need to implement a Spring bean with a method annotated by
@PostConstruct, so that it inserts sample person objects into in memory database while getting up application.
As the last step of implementation, the
person.xhtml page and its corresponding managed bean definition are listed below. The page consists of 2 sections that contain a grid for inputting a new person and a data table for listing the saved persons by retrieving from the database. Table also supports editing and deleting a person from the list with a button and a link.
PersonView managed bean class is a Spring bean and the person repository is injected into it with the
Wrapping it up
To integrate Spring beans into JSF pages, we need to define an EL Resolver in JSF configuration file, which is the
faces-config.xml. The file should be placed under
The web.xml, web application deployment descriptor file, should also be placed under
WEB-INFfolder in order to package the application as a war file. This file is also needed to correctly deploy our application onto Payara Micro. The configuration file should contain the
FacesServlet definition, which is the gateway servlet for handling all of our JSF requests.
As a last step, executing
mvn clean install command within the application folder should create a war archive out of it.
Getting it up and RunningWith Payara Micro it’s so easy to deploy a war archive through the console. Download Payara Micro v4.1.153 from here first and then just execute the command given below.
java -jar payara-micro-4.1.153.jar --deploy %PATH_TO%/ spring-boot-jpa-jsf-example-1.0-SNAPSHOT.warWhen you request for the person JSF page as http://localhost:8080/spring-boot-jpa-jsf-example-1.0-SNAPSHOT/person.jsf, you should be seeing a visual output as follows:
The Bean Validation will be automatically integrated to JSF components so inputting malformed data to the input fields will lead to visual notifications on the page as given below.