Integrating Spring Boot with Java Server Faces using JoinFacesTSEPO MALEKABlockedUnblockFollowFollowingApr 10I was introduced to Spring quite later in my software engineering career.
I began my coding days in the workplace with Java Server Faces, as many would know it with its acronym, JSF.
JSF is a powerful MVC (model-view-controller) Java standard technology used mainly for building component-based and event-driven web interfaces using the simple concept of beans and XHTML pages.
A bean in JSF can both be a controller and a model (or anything, actually).
There is no pre-defined standard in the framework to distinguish between what a model bean is and a controller bean (unlike Spring, for example).
In fact, JSF has left that decision and flexibility to the architects and developers.
Views are well defined as XHTML pages.
XHTML has the same extensive expressions as HTML but strictly conforms to the XML syntax.
XHTML pages allow for the creation of new components that can be reused across your project.
In most cases, applications that make use of the JSF framework often prepare an additional abstraction layer to help them to easily define the differences between what a controller bean is and a model bean — including to control their behaviours.
This flexibility can also open holes for bad design paradigms which can prevent the application from scalability and from being easily maintained.
Once you start working with JSF you will learn that there are certainly missing features you wish came out of the box, which is where Spring comes in.
JSF and Spring are good in their own unique ways.
I personally prefer JSF to build rich and reusable web components and to easily define a standard format for templating a common structure for web interfaces.
On the other hand, JSF offers quite basic bean management and is not as powerful as Spring.
Spring has many great features out of the box.
In this article, I am going to merge the best parts of these two giant Java MVC frameworks for building web applications by integrating JSF with Spring Boot.
Conceptually, integrating JSF into a Spring web application is an ignition for power struggles and control issues of resources.
When we perform this integration, we need to ensure that they both work in harmony and that their roles and participation is well restricted and contained.
The role that JSF will take control of is to render rich web components and we will use it to define a common standard in which our web interfaces will be structured.
Spring will do the rest of the work — to manage my controllers, models, services, repositories (or data access objects) etc.
Getting our hands dirtyNow, let’s get started with a simple tutorial.
Before we start, you can download this project from GitHub or alternately, you can follow through this tutorial to create your own project.
Firstly, we will create a new Spring Boot application using the Spring Initializr tool online.
Once you’ve connected to the Spring Initializr online:Choose to create a Maven ProjectChoose Java as your programming languageAt the time of this article, the version of choice was 2.
4Enter your desired group ID and Artifact ID and any other details you wish to fillWe will now then select the dependencies to include in our Spring Boot project.
For the purpose of this article, we will need only Spring Web MVC and the Embedded Apache Tomcat Server — this comes as a single dependency on Spring Boot.
Importing the Maven projectFor this tutorial, we will be using IntelliJ IDEA to create a new project, import the Maven module and to further write our code.
From IntelliJ IDEA, create a new Empty Project:Once created, we import the Maven project into the project by navigating to the POM file in our file system.
This may take a while as it will be resolving dependencies before the project loads the new module into view.
Adding the JoinFaces dependenciesNow, the next step is to add the JoinFaces dependencies.
JoinFaces is a community project that enables JSF usage in your Spring Boot application by automatically configuring all the JSF libraries you will need to get going, without much fuss.
In our POM file, we will add a dependency for integrating JSF into our Spring Boot application.
Your POM file should now look like this:Configuring some application propertiesThen we will head over to our application.
properties file under the resources folder in our project and make these two entries.
One is to set the server port to 5000 (or any other port you desire, but if you do not specify this property, Spring Boot will automatically use port 8080 — so this property is optional if you still want to use port 8080).
Then we will also turn off Thymeleaf in our application — this is to let Spring know that we will not be using the templating engine in our web pages and therefore it should not enable it.
Now, you can build your project with tests using Maven and if you don’t get any errors, you’re still good to go.
Now, let’s proceed.
Creating our serviceFor this tutorial, we will be creating a simple web page that takes in the year, a month and a day of the month and determines which day of the week it is.
Firstly, we create a service that will perform the core logic to resolve a date to what day of the week it is, as a String type.
We create an interface to define the contract for this service:Then we write the implementation for this service contract, and then use @Service annotation, to let Spring know of the specific type of component this is, a service:Creating form and action beansWe then create a model bean — which we will define as a form bean, by name conversion.
In this example, form beans are models that hold on to the data that will be passed from the web page to the controller.
These form beans will be managed by Spring and not JSF.
In order to let Spring know that it should manage this bean, we should annotate it with @Component annotation.
Then we need to define the scope of the form bean.
For the purpose of this tutorial, our form beans will preferably be defined in the session scope, therefore, we use @SessionScope annotation.
Then we move over to create a controller bean.
This type of bean is an action bean — by name conversion only.
This will become a standard for defining controller beans that perform the actual logic and are normally composed of our form beans.
Our action beans will be also managed by Spring, therefore, we annotate our action beans with @Component.
Then since our action beans do not carry any state or data, we will rather have them scoped for the current request they are needed for, so we annotate our action beans with @RequestScope.
We will also wire the service we created earlier to resolve a date to a day of the week.
Now that we have an action, form and service then we will move on to create our web page.
Creating a JSF pageIn our Maven project, we will need to create a new directory, webapp/WEB-INF, as shown below, which will carry our JSF web pages.
The WEB-INF folder will be used to place web components that are reusable and those that define our template structure (we may not use it in this tutorial).
Under webapp directory in your Maven project, create a new JSF page, and name it, resolve-day-of-week.
Just a few things to note:The DOCTYPE should adhere to HTML5 standards to be able to use the latest HTML5 tags and attributesYou can choose to use Twitter Bootstrap or not — it was just a choice really for me.
So you may not need the stylesheets and the scripts.
The meta tags in the head HTML tag are mandatory, so replace with your own relevant details where applicable.
Now we will modify the JSF page to add a form, with fields to capture the day of the month, the month and the year:Then let’s compile our project and run it.
Once the Spring boot application has started up, you should then see this screen if you navigate to the JSF page from the address bar, as shown:Then we enter the values in the fields and push the Resolve Day of Week button and you should now receive the day of the week, as shown below:Yes, there’s React & Angular for this, right?So, we have achieved our goal, to integrate Spring with JSF.
In this tutorial, I have demonstrated how you can use the powerful web components from JSF to rapidly develop a web application into your Spring Boot application.
This article is well suited for those who come from the JSF background and recently introduced to Spring Boot.
However, if the learning curve is still steep or time-consuming for your project, this can serve as a quick win to bootstrap your project.
Although it is not in the scope of this article, or tutorial, to compare JSF to other modern browser-based MVC frameworks, many developers argue that it is better to use these modern frameworks as they support modularity, independence and rapid development.
It is not, in my opinion, worth the work, to use JSF for a small web project.
Reconsidering your options may be worth your time and benefit.
.. More details