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)

Tags: , , , ,

10 Responses to “Spring Boot with JSF/Primefaces”

  1. bilak September 30, 2015 at 7:11 pm #

    Hi Alex,
    I think you don’t need web.xml at all. Try to look here https://github.com/bilak/spring-boot-primefaces-template (project is not working because there is moyara 2.2.11, but if you downgrade to 2.2.10 everything will work)

  2. Alex October 1, 2015 at 10:29 pm #

    Hi Bilak,

    That’s interesting. If I remove the web.xml from my project it fails on page load, I suspect the discrepancy is something to do with the differences in the pom.xml files between our projects.

    When I get some time I’ll investigate the cause further. Nevertheless, it does no harm to leave it in and if it makes a project less sensitive to classpath changes then all the better.

    Thank you for raising this though.

  3. Marcelo Fernandes February 19, 2016 at 10:54 am #

    Why your pom.xml has following instruction?


    • Alex March 17, 2016 at 11:49 pm #

      This is required because I’m running JSF scoped beans in an embedded Tomcat container and that’s where JSF will scan for them. I think it only happens in Eclipse but it needs checking.

      • Marcelo Fernandes June 11, 2016 at 5:18 pm #

        Alex, I read your valuable article carefully. I think src/main/webapp folder is not necessary with spring boot JAR packaged. JSF resources (xhtml, js, css) should be located at src/main/resources/META-INF/resources folder.

        I put an example app at https://github.com/persapiens/jsf-spring-boot-starter-example

        This example uses jsf-spring-boot-starter that solves JSF and Spring Boot integration issues.

        Best regards.

  4. najd April 12, 2016 at 8:51 am #

    Hi Bilak,,
    when i use postconstruct annotation in my managed bean ,nothing it perform , do you know why and do you have any solution for this problem ?

  5. Serghei May 19, 2016 at 2:11 pm #

    Helpful post, Thank you

  6. Cosmin May 31, 2016 at 4:27 pm #

    Hi Alex,

    Have you tried to build the jar and run it with java -jar boot-faces-1.0-SNAPSHOT.jar ?

    It gives and error:

    Tue May 31 18:22:48 EEST 2016
    There was an unexpected error (type=Internal Server Error, status=500).
    No Factories configured for this Application. This happens if the faces-initialization does not work at all – make sure that you properly include all configuration settings necessary for a basic faces application and that all the necessary libs are included. Also check the logging output of your web application and your container for any exceptions! If you did that and find nothing, the mistake might be due to the fact that you use some special web-containers which do not support registering context-listeners via TLD files and a context listener is not setup in your web.xml. A typical config looks like this; org.apache.myfaces.webapp.StartupServletContextListener

    It works if you have the src folder next to jar. Somehow myfaces is reading in src folder:
    .a.m.c.DefaultFacesConfigurationProvider : Reading config : jar:file:/C:/Users/cosmin.ghita/Downloads/Primedash/src/main/webapp/WEB-INF/lib/atlas-theme-1.1.jar!/META-INF/faces-config.xml

    Do you know how can I fix this?


  7. Lubo August 31, 2016 at 9:58 pm #


    I changed the packaging to WAR and it helped


  1. This Week in Spring – September 29, 2015 | Alexius DIAKOGIANNIS - October 12, 2015

    […] Want to get JSF running on Spring Boot? […]

Leave a Reply

Please solve this equation : (required) Time limit is exhausted. Please reload the CAPTCHA.