Bona Fide Bona Fide - 4 months ago 193
Java Question

Using Spring Boot with JSF Beans

I'm trying to get Spring Boot to work with JSF. The FacesServlet is initialized and the website correctly rendered with Primefaces. But at the point where I call a JSF- or Spring-Bean, nothing is shown.

I know that this question was multiple times asked, but none of them solved my problem. After hours of searching, i don't get it to work. Am I missing something?

Setup:


  • Spring Boot 1.2.7

  • Primefaces 5.2

  • JSF 2.2



What I tried:



What I have

Application.java

@SpringBootApplication
public class Application extends SpringBootServletInitializer {

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

@Bean
public FacesServlet facesServlet() {
return new FacesServlet();
}

@Bean
public ServletRegistrationBean facesServletRegistration() {
ServletRegistrationBean registration = new ServletRegistrationBean(facesServlet(), new String[] { "*.xhtml" });
registration.setName("FacesServlet");
registration.setLoadOnStartup(1);
return registration;
}

@Configuration
static class ConfigureJSFContextParameters implements ServletContextInitializer {

@Override
public void onStartup(ServletContext servletContext) throws ServletException {
servletContext.setInitParameter("com.sun.faces.forceLoadConfiguration", "true");
servletContext.setInitParameter("javax.faces.DEFAULT_SUFFIX", ".xhtml");
servletContext.setInitParameter("encoding", "UTF-8");
}
}

@Bean
public ServletListenerRegistrationBean<ConfigureListener> jsfConfigureListener() {
return new ServletListenerRegistrationBean<ConfigureListener>(new ConfigureListener());
}
}


JsfBean.java

@ManagedBean
public class JsfBean {

private String welcomeMessage = "Populated by JSF created bean";

public String getWelcomeMessage() {
return welcomeMessage;
}
}


SpringBean.java

@Component
public class SpringBean {

private String welcomeMessage = "Populated by spring created bean";

public String getWelcomeMessage() {
return welcomeMessage;
}
}


index.xhtml

<!DOCTYPE html>
<f:view xmlns="http://www.w3.org/1999/xhtml"
xmlns:ui="http://java.sun.com/jsf/facelets"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:c="http://java.sun.com/jsp/jstl/core"
xmlns:p="http://primefaces.org/ui" encoding="UTF-8">

<html>
<h:head>
</h:head>

<h:body>
<h1>Rechnung</h1>
<h3>#{springBean.welcomeMessage}</h3>
<h3>#{jsfBean.welcomeMessage}</h3>
</h:body>
</html>
</f:view>


Output

<h1>Rechnung</h1>
<h3></h3>
<h3></h3>

Answer

I actually got it to work. I overlooked in the linked tutorial, that I have to put the faces-config.xml in the resources-folder src/main/resources/META-INF/ instead of src/main/webapp/META-INF/.

This led to the problem that the el-resolver never was used and this explains, why #{springBean.welcomeMessage} has shown nothing.

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>

<lifecycle>
    <phase-listener>org.springframework.web.jsf.DelegatingPhaseListenerMulticaster</phase-listener>
</lifecycle>