1

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>
2
  • Mixing technologies like JSF and any Spring project will mostly result in a mess. I think it would be better to stick with a full JavaEE stack or completely switch to Spring. Oct 29, 2015 at 11:03
  • @ThomasSchmidt I heard of that problem before but I will consider this way as the last step. I still have a bit hope to do it like I described it above ;-)
    – Bona Fide
    Oct 29, 2015 at 11:42

1 Answer 1

0

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>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.