Spring Boot with JSF; Could not find backup for factory javax.faces.context.FacesContextFactory
To get JSF working on Spring Boot without a web.xml
or faces-config.xml
you need to force it to load its configuration files via an init parameter on the ServletContext
. An easy way to do that is to implement ServletContextAware
:
public class Application implements ServletContextAware {
// ...
@Override
public void setServletContext(ServletContext servletContext) {
servletContext.setInitParameter("com.sun.faces.forceLoadConfiguration", Boolean.TRUE.toString());
}
}
JSF's ConfigureListener
also has a dependency on JSP, so you'll need to add a dependency on Jasper to your pom:
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-jasper</artifactId>
</dependency>
It's not directly related to your problem, but you don't need to declare FacesServlet
as a bean. The ServletRegistrationBean
is sufficient.
This leaves Application.java
looking as follows:
import javax.faces.webapp.FacesServlet;
import javax.servlet.ServletContext;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.context.embedded.ServletListenerRegistrationBean;
import org.springframework.boot.context.embedded.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.context.ServletContextAware;
import com.sun.faces.config.ConfigureListener;
@Configuration
@EnableAutoConfiguration
@ComponentScan
public class Application implements ServletContextAware {
public static void main(String[] args) {
SpringApplication.run(Application.class);
}
@Bean
public ServletRegistrationBean facesServletRegistration() {
ServletRegistrationBean registration = new ServletRegistrationBean(
new FacesServlet(), "*.xhtml");
registration.setLoadOnStartup(1);
return registration;
}
@Bean
public ServletListenerRegistrationBean<ConfigureListener> jsfConfigureListener() {
return new ServletListenerRegistrationBean<ConfigureListener>(
new ConfigureListener());
}
@Override
public void setServletContext(ServletContext servletContext) {
servletContext.setInitParameter("com.sun.faces.forceLoadConfiguration", Boolean.TRUE.toString());
}
}