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.
<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?compilescope and not optional. Apart from that,@ManagedBeandoes 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