Close

Spring Boot JSF Integration

[Last Updated: Sep 10, 2020]

Following example shows how to use JSF with Spring Boot.

Example

pom.xml

pom.xml

<project .....>
<modelVersion>4.0.0</modelVersion>

<groupId>com.logicbig.example</groupId>
<artifactId>boot-jsf-integration-example</artifactId>
<packaging>war</packaging>
<version>1.0-SNAPSHOT</version>

<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.3.3.RELEASE</version>
</parent>

<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>

<dependency>
<groupId>com.sun.faces</groupId>
<artifactId>jsf-api</artifactId>
<version>2.2.20</version>
</dependency>
<dependency>
<groupId>com.sun.faces</groupId>
<artifactId>jsf-impl</artifactId>
<version>2.2.20</version>
</dependency>

<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-jasper</artifactId>
<scope>provided</scope>
</dependency>

</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>2.3.3.RELEASE</version>
<executions>
<execution>
<goals>
<goal>repackage</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>

Creating JSF Management Bean

In Spring Boot application, JSF management Bean can also be declared to be Spring bean.

package com.logicbig.example;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import javax.faces.view.ViewScoped;
import java.time.LocalDateTime;

@Component
@ViewScoped
public class HelloBean {

  @Autowired
  private MsgService msgService;

  public String getMsg() {
      return msgService.getMsg();
  }
}

MsgService

package com.logicbig.example;

import org.springframework.stereotype.Service;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.time.format.FormatStyle;

public interface MsgService {
    String getMsg();

    @Service
    public static class DefaultMsgService implements MsgService {
        @Override
        public String getMsg() {
            return String.format("Hi there!! it's %s here..",
                    LocalDateTime.now().format(
                            DateTimeFormatter.ofLocalizedDateTime(FormatStyle.MEDIUM)));
        }
    }
}

Spring ELResolver

SpringBeanFacesELResolver allows us to use JSF managed beans as Spring beans and also to inject other spring beans there.

src/main/webapp/WEB-INF/faces-config.xml

<?xml version="1.0" encoding="UTF-8"?>
<faces-config xmlns="http://xmlns.jcp.org/xml/ns/javaee"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee/web-facesconfig_2_2.xsd"
	version="2.2">

	<application>
		<el-resolver>org.springframework.web.jsf.el.SpringBeanFacesELResolver
		</el-resolver>
	</application>

</faces-config>

JSF page

src/main/webapp/hello.xhtml

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"
	xmlns:h="http://xmlns.jcp.org/jsf/html"
	xmlns:f="http://xmlns.jcp.org/jsf/core"
	xmlns:ui="http://xmlns.jcp.org/jsf/facelets">
<h:head>
</h:head>

<h:body>
	<h:form>
		<h:outputText id="hello-output" value="#{helloBean.msg}" />
		<br/>

	</h:form>
</h:body>
</html>

The Main class

Other than being main spring boot class, this class also registers JSF's FacesServlet via ServletRegistrationBean.

package com.logicbig.example;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import javax.faces.webapp.FacesServlet;
import javax.servlet.ServletContext;
import java.util.Arrays;

@SpringBootApplication
public class Main{

  public static void main (String[] args) {
      SpringApplication.run(Main.class, args);
  }

  @Bean
  ServletRegistrationBean jsfServletRegistration (ServletContext servletContext) {
      //spring boot only works if this is set
      servletContext.setInitParameter("com.sun.faces.forceLoadConfiguration", Boolean.TRUE.toString());

      //FacesServlet registration
      ServletRegistrationBean srb = new ServletRegistrationBean();
      srb.setServlet(new FacesServlet());
      srb.setUrlMappings(Arrays.asList("*.xhtml"));
      srb.setLoadOnStartup(1);
      return srb;
  }
}

Running application

mvn spring-boot:run
$ mvn -q spring-boot:run

  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::        (v2.3.3.RELEASE)

2020-09-10 23:12:46.375  INFO 10352 --- [           main] com.logicbig.example.Main                : Starting Main on DESKTOP-0E87003 with PID 10352 (D:\LogicBig\example-projects\spring-boot\boot-jsf-integration\boot-jsf-integration-example\target\classes started by joe in D:\LogicBig\example-projects\spring-boot\boot-jsf-integration\boot-jsf-integration-example)
2020-09-10 23:12:46.377  INFO 10352 --- [           main] com.logicbig.example.Main                : No active profile set, falling back to default profiles: default
2020-09-10 23:12:46.938  INFO 10352 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat initialized with port(s): 8080 (http)
2020-09-10 23:12:46.947  INFO 10352 --- [           main] o.apache.catalina.core.StandardService   : Starting service [Tomcat]
2020-09-10 23:12:46.948  INFO 10352 --- [           main] org.apache.catalina.core.StandardEngine  : Starting Servlet engine: [Apache Tomcat/9.0.37]
2020-09-10 23:12:47.132  INFO 10352 --- [           main] org.apache.jasper.servlet.TldScanner     : At least one JAR was scanned for TLDs yet contained no TLDs. Enable debug logging for this logger for a complete list of JARs that were scanned but no TLDs were found in them. Skipping unneeded JARs during scanning can improve startup time and JSP compilation time.
2020-09-10 23:12:47.152  INFO 10352 --- [           main] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring embedded WebApplicationContext
2020-09-10 23:12:47.153  INFO 10352 --- [           main] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 742 ms
2020-09-10 23:12:47.214  INFO 10352 --- [           main] j.e.resource.webcontainer.jsf.config     : Initializing Mojarra 2.2.20 ( 20190731-0757 59754ac80c05d61848a08939ddd11a324f2345ac) for context ''
2020-09-10 23:12:47.288  INFO 10352 --- [           main] j.e.r.webcontainer.jsf.application       : JSF1048: PostConstruct/PreDestroy annotations present.  ManagedBeans methods marked with these annotations will have said annotations processed.
2020-09-10 23:12:47.587  INFO 10352 --- [           main] o.s.s.concurrent.ThreadPoolTaskExecutor  : Initializing ExecutorService 'applicationTaskExecutor'
2020-09-10 23:12:47.695  INFO 10352 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat started on port(s): 8080 (http) with context path ''
2020-09-10 23:12:47.703  INFO 10352 --- [           main] com.logicbig.example.Main                : Started Main in 1.575 seconds (JVM running for 1.803)

Example Project

Dependencies and Technologies Used:

  • Spring Boot 2.3.3.RELEASE
  • spring-boot-starter-web : Starter for building web, including RESTful, applications using Spring MVC. Uses Tomcat as the default embedded container.
  • jsf-api 2.2.20: This is the master POM file for Oracle's Implementation of the JSF 2.2 Specification.
  • jsf-impl 2.2.20: This is the master POM file for Oracle's Implementation of the JSF 2.2 Specification.
  • tomcat-embed-jasper 9.0.37: Core Tomcat implementation.
  • JDK 1.8
  • Maven 3.5.4

Spring Boot JSF Integration Select All Download
  • boot-jsf-integration-example
    • src
      • main
        • java
          • com
            • logicbig
              • example
                • HelloBean.java
          • webapp
            • WEB-INF

    See Also