1

I'm trying to implement a webapp using Spring Boot and JSF 2.2.

I've managed to set up Spring Boot correctly and I can display normal HTML with a controller. But XHTML pages are not rendering correctly.

I believe it is to do with my configuration but I cannot figure out what it is.

I'm using IntelliJ IDEA Ultimate 2018.1 on macOS High Sierra.

Here is my pom.xl

<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>group</groupId> <artifactId>spring-jsf-webapp</artifactId> <version>1.0-SNAPSHOT</version> <packaging>war</packaging> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.0.3.RELEASE</version> </parent> <properties> <start-class>com.latwoa.Application</start-class> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-devtools</artifactId> <optional>true</optional> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-tomcat</artifactId> <scope>provided</scope> </dependency> <dependency> <groupId>com.sun.faces</groupId> <artifactId>jsf-api</artifactId> <version>2.2.17</version> <scope>provided</scope> </dependency> <dependency> <groupId>com.sun.faces</groupId> <artifactId>jsf-impl</artifactId> <version>2.2.17</version> <scope>compile</scope> <optional>true</optional> </dependency> </dependencies> <build> <plugins> <!-- Package as an executable jar/war --> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project> 

My web.xml:

<?xml version="1.0" encoding="UTF-8"?> <web-app 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 http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd" version="4.0"> <servlet> <servlet-name>Faces Servlet</servlet-name> <servlet-class>javax.faces.webapp.FacesServlet</servlet-class> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>Faces Servlet</servlet-name> <url-pattern>*.xhtml</url-pattern> </servlet-mapping> <welcome-file-list> <welcome-file>index.xhtml</welcome-file> </welcome-file-list> </web-app> 

My faces-config.xml:

<?xml version='1.0' encoding='UTF-8'?> <faces-config version="2.2" 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 http://xmlns.jcp.org/xml/ns/javaee/web-facesconfig_2_2.xsd"> <application> <el-resolver>org.springframework.web.jsf.el.SpringBeanFacesELResolver</el-resolver> </application> </faces-config> 

As well as the JSF bean:

import javax.faces.bean.ManagedBean; import javax.faces.bean.RequestScoped; @ManagedBean @RequestScoped public class JsfBean { private String welcomeMessage = "Populated by JSF created bean"; public String getWelcomeMessage() { return welcomeMessage; } } 

And the XHTML page:

<!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml" xmlns:ui="http://java.sun.com/jsf/facelets" xmlns:h="http://xmlns.jcp.org/jsf/html" xmlns:f="http://xmlns.jcp.org/jsf/core" xmlns:jsf="http://xmlns.jcp.org/jsf"> <f:view> <h:head></h:head> <!--Main Body --> <h:body> <h1>Welcome to JSF spring boot tutorial</h1> <h:outputText value="foo bar" /> <h3>#{jsfBean.welcomeMessage}</h3> </h:body> </f:view> </html> 

Like I mentioned above, the <h1> displays fine. But <h:outputText value="foo bar" /> and #{jsfBean.welcomeMessage} are not rendered.

4
  • Did you look at the html source on the client? And then look for duplicate questions. I think (99% sure) it is because you include the jsf api but not the impl Commented Jun 22, 2018 at 18:48
  • @Kukeltje I have. The source shows the literal <h:outputText /> tag. I do have jsf-impl in my dependencies, but I hadn't noticed the <optional>true</optional> tag. Could that be causing the error? Commented Jun 22, 2018 at 19:50
  • Sorry, I mixed them up. Would not suspect the optional to be relevant but you could try. And spring boot does already contain the jsf api? Commented Jun 22, 2018 at 20:31
  • Spring Boot does not include neither JSF api or the impl. You need both dependencies with the compile scope and not optional. Apart from that, @ManagedBean does not work with Spring Boot (I couldn't have it work myself, at least). Here you've what I did to configure it with Spring Boot 1: stackoverflow.com/a/46190826/1199132 Commented Jun 25, 2018 at 7:18

1 Answer 1

1

Outined here https://dzone.com/articles/developing-jsf-applications-with-spring-boot

You seem to be missing the step to configure JSF with Spring Boot

@EnableAutoConfiguration @ComponentScan({"com.auth0.samples.bootfaces"}) public class Application extends SpringBootServletInitializer { public static void main(String[] args) { SpringApplication.run(Application.class, args); } @Bean public ServletRegistrationBean servletRegistrationBean() { FacesServlet servlet = new FacesServlet(); return new ServletRegistrationBean(servlet, "*.jsf"); } } 

I'd also add these listeners to integrate spring context with your managed beans:

 <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <listener> <listener-class>org.springframework.web.context.request.RequestContextListener</listener-class> </listener> 
Sign up to request clarification or add additional context in comments.

2 Comments

I have looked at that tutorial before, and adding that @Bean to my Application breaks things more by giving me this error: Whitelabel Error Page This application has no explicit mapping for /error, so you are seeing this as a fallback. Fri Jun 22 20:53:42 BST 2018 There was an unexpected error (type=Internal Server Error, status=500). Could not find backup for factory javax.faces.context.FacesContextFactory. I could not find a fix for 'Could not find backup for factory javax.faces.context.FacesContextFactory.'

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.