SpringBoot与PrimeFaces简单整合
简单例子
pom.xml
<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.4.2</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>primefaces</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>primefaces</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.primefaces</groupId>
<artifactId>primefaces</artifactId>
<version>8.0</version>
</dependency>
<dependency>
<groupId>com.sun.faces</groupId>
<artifactId>jsf-api</artifactId>
<version>2.2.20</version>
</dependency>
<dependency>
<groupId>com.sun.faces</groupId>
<artifactId>jsf-impl</artifactId>
<version>2.2.20</version>
</dependency>
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-jasper</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-test</artifactId>
<version>2.4.2</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.sonatype.sisu</groupId>
<artifactId>sisu-inject-bean</artifactId>
<version>1.4.2</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
受托管的Bean
在SpringBoot应用中,受JSF托管的Bean也是Spring的Bean。
package com.example.primefaces.pojo;
import com.example.primefaces.service.DataService;
import org.primefaces.model.chart.Axis;
import org.primefaces.model.chart.AxisType;
import org.primefaces.model.chart.LineChartModel;
import org.primefaces.model.chart.LineChartSeries;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import javax.annotation.PostConstruct;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
@Component
@ManagedBean
@ViewScoped
public class LineChartBean {
@Autowired
private DataService dataService;
private LineChartModel lineModel;
@PostConstruct
public void init() {
lineModel = new LineChartModel();
LineChartSeries s = new LineChartSeries();
s.setLabel("Population");
dataService.getLineChartData().forEach(s::set);
lineModel.addSeries(s);
lineModel.setLegendPosition("e");
Axis y = lineModel.getAxis(AxisType.Y);
y.setMin(0.5);
y.setMax(700);
y.setLabel("Millions");
Axis x = lineModel.getAxis(AxisType.X);
x.setMin(0);
x.setMax(7);
x.setTickInterval("1");
x.setLabel("Number of Years");
}
public LineChartModel getLineModel() {
return lineModel;
}
}
服务类DataService
package com.example.primefaces.service;
import org.springframework.stereotype.Service;
import java.util.LinkedHashMap;
import java.util.Map;
@Service
public class DataService {
public Map<Integer, Double> getLineChartData() {
Map<Integer, Double> map = new LinkedHashMap<>();
map.put(1, 5.20);
map.put(2, 19.63);
map.put(3, 59.01);
map.put(4, 139.76);
map.put(5, 300.4);
map.put(6, 630.0);
return map;
}
}
配置文件Spring ELResolver
SpringBeanFacesELResolver将受JSF托管的Bean作为Spring的Bean,同时也可以注入其他Spring的Bean。
路径:src/main/webapp/WEB-INF/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>
</faces-config>
JSF 页面
路径:src/main/webapp/index.xhtml
<?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"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:p="http://primefaces.org/ui"
xmlns:ui="http://java.sun.com/jsf/facelets">
<h:head>
</h:head>
<h:body style="margin-left:50px">
<h2>PrimeFaces Linear Chart Example</h2>
<p:chart type="line" model="#{lineChartBean.lineModel}" style="height:400px;width:600px"/>
</h:body>
</html>
SpringBoot启动类
package com.example.primefaces;
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.Arrays;
@SpringBootApplication
public class PrimefacesApplication {
public static void main(String[] args) {
SpringApplication.run(PrimefacesApplication.class, args);
}
@Bean
ServletRegistrationBean jsfServletRegistration (ServletContext servletContext) {
//只有设置了这个SpringBoot才会生效
servletContext.setInitParameter("com.sun.faces.forceLoadConfiguration", Boolean.TRUE.toString());
//注册
ServletRegistrationBean srb = new ServletRegistrationBean();
srb.setServlet(new FacesServlet());
srb.setUrlMappings(Arrays.asList("*.xhtml"));
srb.setLoadOnStartup(1);
return srb;
}
}
启动
之后打开浏览器在地址栏输入http://localhost:8080/index.xhtml