问题:
Spring 没有配置JSF 的文档 该如何配置spring boot JSF
解决:
https://github.com/giant35/springbootjsf
有几个关键点: 1. pom.xml 加 jsf-impl & jsf-api & tomcat-embed-jasper &jstl;2. 注册JSF Servlet ;3. 添加faces-config.xml ; 4. 添加 xhtml 页面 ; 5. 添加 Beans
- pom 添加JSF
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859<?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>com.enjoylearn</groupId><artifactId>springbootjsf</artifactId><version>1.0-SNAPSHOT</version><packaging>jar</packaging><properties><project.build.sourceEncoding>UTF-8</project.build.sourceEncoding><maven.compiler.source>1.8</maven.compiler.source><maven.compiler.target>1.8</maven.compiler.target></properties><parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>2.0.0.M7</version></parent><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></dependency><dependency><groupId>com.sun.faces</groupId><artifactId>jsf-impl</artifactId><version>2.2.8-02</version></dependency><dependency><groupId>com.sun.faces</groupId><artifactId>jsf-api</artifactId><version>2.2.8-02</version></dependency><dependency><groupId>org.apache.tomcat.embed</groupId><artifactId>tomcat-embed-jasper</artifactId></dependency><dependency><groupId>jstl</groupId><artifactId>jstl</artifactId><version>1.2</version></dependency></dependencies><repositories><repository><id>spring-milestones</id><name>Spring Milestones</name><url>https://repo.spring.io/libs-milestone</url><snapshots><enabled>false</enabled></snapshots></repository></repositories></project>
- 提供 ServletRegistrationBean Bean
1234567891011121314151617181920212223242526272829303132333435363738package com.enjoylearn.springbootjsf;import com.sun.faces.config.ConfigureListener;import javax.faces.webapp.FacesServlet;import javax.servlet.ServletContext;import org.springframework.boot.web.servlet.ServletListenerRegistrationBean;import org.springframework.boot.web.servlet.ServletRegistrationBean;import org.springframework.context.annotation.Bean;import org.springframework.context.annotation.Configuration;import org.springframework.web.context.ServletContextAware;/**** @author sihai*/@Configurationpublic class JSFConfig implements ServletContextAware{@Beanpublic ServletRegistrationBean facesServletRegistration() {ServletRegistrationBean registration = new ServletRegistrationBean(new FacesServlet(), "*.jsf");registration.setLoadOnStartup(1);return registration;}@Beanpublic ServletListenerRegistrationBean<ConfigureListener> jsfConfigureListener() {return new ServletListenerRegistrationBean<>(new ConfigureListener());}@Overridepublic void setServletContext(ServletContext sc) {sc.setInitParameter("com.sun.faces.forceLoadConfiguration", Boolean.TRUE.toString());}}
- src/main/webapp/WEB-INF/faces-config.xml
123456789<?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></faces-config>
- src/main/webapp/test2.xhtml
12345678910<?xml version='1.0' encoding='UTF-8' ?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><title>Facelet Title</title></head><body>Hello from Facelets222 #{hh.msg}</body></html>
- src/main/java/***/ HelloController.java
123456789101112131415package com.enjoylearn.springbootjsf;import org.springframework.context.annotation.Bean;import org.springframework.stereotype.Component;/**** @author sihai*/@Component("hh")public class HelloController {public String getMsg(){return "你好啊啊";}}
- App.java
123456789101112131415package com.enjoylearn.springbootjsf;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;/**** @author sihai*/@SpringBootApplicationpublic class App {public static void main(String[] argv){SpringApplication.run(App.class, argv);}}
参考:
https://www.beyondjava.net/blog/jsf-2-2-primefaces-5-spring-boot/
https://stackoverflow.com/questions/25479986/spring-boot-with-jsf-could-not-find-backup-for-factory-javax-faces-context-face?noredirect=1