I\'m having some problems with Spring Boot and JSF. The servlet appears to start up correctly, but when I attempt to access a resource I get the following exception
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:
org.apache.tomcat.embed
tomcat-embed-jasper
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 jsfConfigureListener() {
return new ServletListenerRegistrationBean(
new ConfigureListener());
}
@Override
public void setServletContext(ServletContext servletContext) {
servletContext.setInitParameter("com.sun.faces.forceLoadConfiguration", Boolean.TRUE.toString());
}
}