这里主要用两个primefaces官网案例来介绍与SpringBoot的整合
官网:https://www.primefaces.org/">https://www.primefaces.org/
项目结构
Maven依赖
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- https://mvnrepository.com/artifact/org.primefaces/primefaces -->
<dependency>
<groupId>org.primefaces</groupId>
<artifactId>primefaces</artifactId>
<version>10.0.0-RC1</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.sun.faces/jsf-api -->
<dependency>
<groupId>com.sun.faces</groupId>
<artifactId>jsf-api</artifactId>
<version>2.2.20</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.sun.faces/jsf-impl -->
<dependency>
<groupId>com.sun.faces</groupId>
<artifactId>jsf-impl</artifactId>
<version>2.2.20</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.tomcat.embed/tomcat-embed-jasper -->
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-jasper</artifactId>
<version>8.5.20</version>
<scope>provided</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/org.sonatype.sisu/sisu-inject-bean -->
<dependency>
<groupId>org.sonatype.sisu</groupId>
<artifactId>sisu-inject-bean</artifactId>
<version>2.3.0</version>
</dependency>
</dependencies>
InventoryStatus.java
package com.daniel.model.menu;
/**
* @Author Daniel
* @Description 库存状态
**/
public enum InventoryStatus {
INSTOCK("In Stock"),
OUTOFSTOCK("Out of Stock"),
LOWSTOCK("Low Stock");
private String text;
InventoryStatus(String text) {
this.text = text;
}
public String getText() {
return text;
}
}
PrimeFacesApplication.java
package com.daniel;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import javax.faces.webapp.FacesServlet;
import javax.servlet.ServletContext;
import java.util.Collections;
/**
* @Author Daniel
* @Description 启动类,初始化JSF
**/
@SpringBootApplication
public class PrimeFacesApplication {
public static void main(String[] args) {
SpringApplication.run(PrimeFacesApplication.class, args);
}
@Bean
public ServletRegistrationBean<FacesServlet> jsfServletRegistration(ServletContext servletContext) {
// JSF的配置
servletContext.setInitParameter("com.sun.faces.forceLoadConfiguration", Boolean.TRUE.toString());
// 注册FacesServlet
ServletRegistrationBean<FacesServlet> srb = new ServletRegistrationBean<>();
srb.setServlet(new FacesServlet());
srb.setUrlMappings(Collections.singletonList("*.xhtml"));
srb.setLoadOnStartup(1);
return srb;
}
}
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>
<!--SpringBoot整合JSF框架的管理Bean对象 EL表达式-->
<el-resolver>org.springframework.web.jsf.el.SpringBeanFacesELResolver</el-resolver>
</application>
</faces-config>
效果展示:
http://localhost:8080/bar.xhtml">http://localhost:8080/bar.xhtml
http://localhost:8080/contextMenu.xhtml">http://localhost:8080/contextMenu.xhtml