Tag Archives: primefaces
Spring Boot with JSF and Primefaces

Spring Boot with JSF/Primefaces

I’m a big fan of Spring Boot. If you need to bootstrap a Java Enterprise application in a short space of time its an excellent project to get you moving and within a few lines you can expose or consume RESTful services, initiate an embedded Tomcat servlet container and auto-configure your way into the whole Spring ecosystem with barely a line of XML in site (yes, there’s still the Maven POM).

This leads us to a new application design paradigm, that of the micro-service or application. Each web app in a self-contained jar, its own embedded web-server with its own configuration running in its own JVM instance sitting behind a web proxy presenting a suite or cloud of services to the outside world seemingly as a single product.

You can of course use Spring Web with Thymeleaf or simple JSP with AngularJS but I wanted to demonstrate how to build a web interface to such a service using JSF and Primefaces and have posted a fully functional demo application to GitHub:

This an Eclipse Maven Spring Boot project, get it built and running and navigate to http://localhost:9090 and have a look at its behaviour. The data is also exposed as a RESTful service using Spring Web’s @RestController at http://localhost:9090/service/books/ and individual items on http://localhost:9090/service/book/0/.

The embedded Tomcat container has very little functionality of its own, JSF capabilities are bestowed by adding the appropriate JSF and Primefaces dependencies to the Maven POM followed by additional annotated bean definitions to augment the Spring Boot auto-configuration. Although Tomcat is using java config it still requires the presence of a web.xml file and a faces-config.xml file in the webapp/WEB-INF directory. Any configuration that would normally be done in the web.xml file is done in a class that implements ServletContextInitializer such as our class Initializer in the example which must in turn be announced to the Spring Boot SpringApplicationBuilder during bootstrap.

We now have two different types of containers active, one Spring container and a J2EE Servlet container (Tomcat) and really its better to keep them separate in your mind and technically but if we are going to transfer values between them we need some sort of interop bridge. Spring provides this through expression language via a SpringBeanFacesELResolver which is referenced in your faces-config.xml and injected by Spring for you. Now you can access Spring beans in your managed faces bean, passing vales both ways. See class BookModel.

Everything else in the project is simply a demonstration of the power of Spring Boot and how easy it is to add features thanks to auto configuration. The project includes:

  • Spring Data JPA
  • Spring Rest
  • Spring Actuator
  • Jackson Repository Populators
  • URL Rewriting

Details of all of these can be found in the Spring Boot documentation except the last item, URL Rewriting. The one thing I hate about JSF is the ugly URL schema it leaves you with. This can be remedied by adding OcpSoft’s Rewrite Project to your project. I’m bringing this up now because once you’ve added the dependency to your POM the configuration requires a little special Spring Boot configuration voodoo too.

If you recall I mentioned that the web.xml file is not loaded by the embedded Tomcat server and that all configuration is done through java config, well that includes activating the RewriteFilter bean. This is performed in the class that extends SpringBootServletInitializer:

From here on you just need to define a HttpConfigurationProvider and annotate it with the @RewriteConfiguration annotation as per the documentation.

See also: The AngularFaces Project See Also: JSF on Spring Boot (Understanding Scopes)

Comments ( 11 )