Learn how to integrate JSF with spring boot to allow common configuration across applications
Detailed Video Notes
If you work in a large organization you may have hundred of applications that are JSF based it is unlikely you will flip a switch to move away from them. Spring boot does a very good job separating concerns and supporting various templating languages that are more familiar to front end developers. While you maintain a JSF stack you may want to take advantage of spring boot features so in this tutorial we will show how to integrate spring boot and jsf 2.2.
If you are not aware JSF 2.2 has many new features such as HTML5 support, allows a more natural templating with pass through attributes, security enhancements and much more. I still am not a fan of java code producing HTML, why not just write it. This java tutorial will focus on sun's mojarra so depending on which implementation (Apache MyFaces) there could be subtle differences.
Project set up
Following the spring boot tutorial and creating a project from spring initializr website we will select web and generate the project. After downloading the project we will import it into eclipse and update the dependencies we need to configure JSF.
Configuring faces servlet
JSF is just a servlet so both JSF and spring MVC can co-exist and when they do it makes for friendly way to expose REST application services that sit right next to your application. Creating a configuration class
ConfigureJSF we will register faces servlet with spring boot by creating
ServletRegistrationBean which has a spring friendly design to register servlets in Servlet 3.0+ containers. When spring boot starts up it will automatically find any type of
ServletRegistrationBean and register it with your container. You will notice that we extend
ServletRegistrationBean in order grab hold of
FacesInitializer and pass in servletContext to enable us to set application configuration parameters which we will show later.
Since JSF 2 supports annotations the dependency on the faces-config.xml diminishes but we still have the need to register spring's el-resolver and phase listener. If you aren't familiar
SpringBeanFacesELResolver allows you to use spring beans in JSF context and
DelegatingPhaseListenerMulticaster delegates to spring
WebApplicationContext. With this set up an application can use either spring manage beans or JSF annotations such as
@ManagedBean. We will create faces-config.xml in in the
src/main/resources/META-INF/faces-config.xml and add the el-resolver and phase-listener.
Creating sample page
To demonstrate that a JSF xhtml file can reference both a spring bean and a JSF managed bean we will create index.xhtml file in spring boot default location
src/main/webapp. At this point we are ready to run our application and access http://localhost:8080/index.faces.
JSF managed bean
Template source code
Template generated output
JSF context parameters
Spring boot incorporates many servlet 3.0 features which will be an adjustment for developers as a web.xml isn't needed anymore. JavaServer Faces is configured using context parameters so we need to initializing servlet parameters programmatically using spring. To do this we will create a class that extends
ServletContextInitializer a spring boot class that allows you to configure a Servlet 3.0+ context programmatically. Next since JSF has configuration that can be applied based on environment we will use
@Profile and set spring.profiles.active=dev in application.properties. In our snippet what this will do is when we are in a development profile the JSF templates will refresh regularly vs in production we wouldn't.
Thanks for joining in today's level up, have a great day!