Primefaces JoinFaces (一)

JoinFaces 简单示例

官网地址

JoinFaces是什么?

  1. 此项目集成最新的Springboot、JSF。
  2. 自动配置Primefaces、PrimeFaces Extensions、AdminFaces、BootsFaces 等。
  3. 当前版本支持JSF和Springboot特性,支持JSF和CDI注解,支持JSF Facelet标签。

JoinFaces 如何使用?

通过maven下载JoinFaces
pom.xml

1
2
3
4
5
6
7
8
9
10
11
12
13
<parent>
<groupId>org.joinfaces</groupId>
<artifactId>joinfaces-parent</artifactId>
<version>3.2.4</version>
<relativePath/>
</parent>

<dependencies>
<dependency>
<groupId>org.joinfaces</groupId>
<artifactId>jsf-spring-boot-starter</artifactId>
</dependency>
</dependencies>

JoinFaces 启动器

版本:JoinFaces Starters 3.x
有16个JoinFaces启动器可供选择:六个基本启动器、两个实用程序启动器、一个元启动器、五个组件启动器、一个主题启动器和一个额外启动器。

这里只介绍 Primefaces,其他请看官网https://github.com/joinfaces/joinfaces/wiki/Joinfaces-Starters-3.x

Starter Description
primefaces-spring-boot-starter Component starter for PrimeFaces 6.2, and PrimeFaces Extensions 6.2.4 JSF Component libraries. Pulls jsf-spring-boot-starter.

JSF和CDI注释自动支持

注解 文档
@NoneScoped https://docs.oracle.com/javaee/7/api/javax/faces/bean/NoneScoped.html
@RequestScoped https://docs.oracle.com/javaee/7/api/javax/enterprise/context/RequestScoped.html
@ViewScoped https://docs.oracle.com/javaee/7/api/javax/faces/view/ViewScoped.html
@SessionScoped https://docs.oracle.com/javaee/7/api/javax/enterprise/context/SessionScoped.html
@ApplicationScoped https://docs.oracle.com/javaee/7/api/javax/enterprise/context/ApplicationScoped.html

Primefaces 实战(零XML配置)

Maven

pom.xml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
<!-- joinfaces -->
<parent>
<groupId>org.joinfaces</groupId>
<artifactId>joinfaces-parent</artifactId>
<version>3.2.4</version>
<relativePath/>
</parent>

<dependencies>
<!-- Primefaces -->
<dependency>
<groupId>org.joinfaces</groupId>
<artifactId>primefaces-spring-boot-starter</artifactId>
</dependency>

<!--Named-->
<dependency>
<groupId>javax.inject</groupId>
<artifactId>javax.inject</artifactId>
<version>1</version>
</dependency>
</dependencies>

实体类

HelloWorld.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
package com.ray.primefaces02.model;

/**
* @author Ray
* @date 2018/9/7 0007
* 实体类
*/
public class HelloWorld {

private String firstName;
private String lastName;

public String getFirstName() {
return firstName;
}

public void setFirstName(String firstName) {
this.firstName = firstName;
}

public String getLastName() {
return lastName;
}

public void setLastName(String lastName) {
this.lastName = lastName;
}

public HelloWorld(String firstName, String lastName) {
this.firstName = firstName;
this.lastName = lastName;
}

public String showGreeting(){
return "Hello " + firstName + " " + lastName + "!";
}
}

JSF 视图

HelloView.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
package com.ray.primefaces02.view;

import com.ray.primefaces02.model.HelloWorld;

import javax.annotation.PostConstruct;
import javax.inject.Named;

/**
* @author Ray
* @date 2018/9/7 0007
* 视图数据
* 使用 @Named 这样它就编程了一个CDI托管bean
* 它的EL名称可以被JSF框架访问
*/
@Named
public class HelloView {

private HelloWorld helloWorld;

@PostConstruct
public void init(){
helloWorld = new HelloWorld("Remember","Ray");
}

public HelloWorld getHelloWorld() {
return helloWorld;
}

public void setHelloWorld(HelloWorld helloWorld) {
this.helloWorld = helloWorld;
}
}

JSF 页面

hello.xhtml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
<?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">

<h:head>
<title>PrimeFaces Hello World Example</title>
</h:head>

<h:body>
<h:form id="helloworld-form">
<p:panel header="PrimeFaces Hello World Example">
<h:panelGrid columns="2" cellpadding="4">
<h:outputText value="First Name: " />
<p:inputText id="first-name" value="#{helloView.helloWorld.firstName}" />

<h:outputText value="Last Name: " />
<p:inputText id="last-name" value="#{helloView.helloWorld.lastName}" />

<p:commandButton id="submit" value="Submit" update="greeting-panel" oncomplete="PF('greetingDialog').show()" />
</h:panelGrid>
</p:panel>

<p:dialog header="Message" widgetVar="greetingDialog" modal="true" resizable="false">
<h:panelGrid id="greeting-panel" columns="1" cellpadding="4">
<h:outputText value="#{helloView.helloWorld.showGreeting()}" />
</h:panelGrid>
</p:dialog>
</h:form>
</h:body>
</html>

运行效果

启动Tomcat运行项目,请注意查看启动信息,如果有异常应该先解决异常信息,运行成功后的结果如下所示:
访问地址:http://localhost:8080/hello.xhtml

-------------- 本文结束  感谢您的阅读 --------------
0%