JSF - 复合组件
JSF 为开发人员提供了定义自己的自定义组件的强大功能,这些组件可用于呈现自定义内容。
定义自定义组件
在 JSF 中定义自定义组件是一个两步过程。
| 步 | 描述 |
|---|---|
| 1a | 创建一个资源文件夹。 在资源文件夹中创建一个具有复合命名空间的 xhtml 文件。 |
| 1b | 使用复合标签composite:interface、composite:attribute和composite:implementation来定义复合组件的内容。在composite:implementation中使用cc.attrs来获取使用composite :interface中的composite :attribute定义的变量。 |
步骤 1a:创建自定义组件:loginComponent.xhtml
在resources文件夹中创建一个文件夹tutorialspoint,并在其中创建一个文件loginComponent.xhtml。
在 html header 中使用复合命名空间。
<html xmlns = "http://www.w3.org/1999/xhtml" xmlns:h = "http://java.sun.com/jsf/html" xmlns:f = "http://java.sun.com/jsf/core" xmlns:composite = "http://java.sun.com/jsf/composite"> ... </html>
步骤 1b:使用复合标签:loginComponent.xhtml
下表描述了复合标签的使用。
| 序列号 | 标签和描述 |
|---|---|
| 1 | 复合:界面 声明要在composite:implementation中使用的可配置值。 |
| 2 | 复合:属性 配置值使用此标签声明。 |
| 3 | 复合:实现 声明 JSF 组件。可以使用#{cc.attrs.attribute-name}表达式访问composite:interface中定义的可配置值。 |
<composite:interface>
<composite:attribute name = "usernameLabel" />
<composite:attribute name = "usernameValue" />
</composite:interface>
<composite:implementation>
<h:form>
#{cc.attrs.usernameLabel} :
<h:inputText id = "username" value = "#{cc.attrs.usernameValue}" />
</h:form>
使用自定义组件
在 JSF 中使用自定义组件是一个简单的过程。
| 步 | 描述 |
|---|---|
| 2a | 创建一个 xhtml 文件并使用自定义组件的命名空间。命名空间将为http://java.sun.com/jsf/<folder-name>,其中folder-name是包含自定义组件的资源目录中的文件夹 |
| 2b | 将自定义组件用作普通 JSF 标记 |
步骤 2a:使用自定义命名空间:home.xhtml
<html xmlns = "http://www.w3.org/1999/xhtml" xmlns:h = "http://java.sun.com/jsf/html" xmlns:ui = "http://java.sun.com/jsf/facelets"> xmlns:tp = "http://java.sun.com/jsf/composite/tutorialspoint">
步骤 2b:使用自定义标签:home.xhtml 并传递值
<h:form>
<tp:loginComponent
usernameLabel = "Enter User Name: "
usernameValue = "#{userData.name}" />
</h:form>
应用示例
让我们创建一个测试 JSF 应用程序来测试 JSF 中的自定义组件。
| 步 | 描述 |
|---|---|
| 1 | 按照JSF - 第一个应用程序章节中的说明,在com.tutorialspoint.test包下创建一个名为helloworld的项目。 |
| 2 | 在src → main文件夹下创建resources文件夹。 |
| 3 | 在src→main→resources文件夹下创建tutorialspoint文件夹。 |
| 4 | 在src→main→resources→tutorialspoint文件夹下创建loginComponent.xhtml文件。 |
| 5 | 如下所述修改UserData.java文件。 |
| 6 | 按如下所述修改home.xhtml 。其余文件保持不变。 |
| 7 | 编译并运行应用程序以确保业务逻辑按照要求运行。 |
| 8 | 最后,以war文件的形式构建应用程序并将其部署在Apache Tomcat Web服务器中。 |
| 9 | 使用适当的 URL 启动您的 Web 应用程序,如下面最后一步中所述。 |
登录组件.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:f = "http://java.sun.com/jsf/core"
xmlns:composite = "http://java.sun.com/jsf/composite">
<composite:interface>
<composite:attribute name = "usernameLabel" />
<composite:attribute name = "usernameValue" />
<composite:attribute name = "passwordLabel" />
<composite:attribute name = "passwordValue" />
<composite:attribute name = "loginButtonLabel" />
<composite:attribute name = "loginButtonAction"
method-signature = "java.lang.String login()" />
</composite:interface>
<composite:implementation>
<h:form>
<h:message for = "loginPanel" style = "color:red;" />
<h:panelGrid columns = "2" id = "loginPanel">
#{cc.attrs.usernameLabel} :
<h:inputText id = "username" value = "#{cc.attrs.usernameValue}" />
#{cc.attrs.passwordLabel} :
<h:inputSecret id = "password" value = "#{cc.attrs.passwordValue}" />
</h:panelGrid>
<h:commandButton action = "#{cc.attrs.loginButtonAction}"
value = "#{cc.attrs.loginButtonLabel}"/>
</h:form>
</composite:implementation>
</html>
用户数据.java
package com.tutorialspoint.test;
import java.io.Serializable;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
@ManagedBean(name = "userData", eager = true)
@SessionScoped
public class UserData implements Serializable {
private static final long serialVersionUID = 1L;
private String name;
private String password;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String login() {
return "result";
}
}
首页.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:f = "http://java.sun.com/jsf/core"
xmlns:tp = "http://java.sun.com/jsf/composite/tutorialspoint">
<h:head>
<title>JSF tutorial</title>
</h:head>
<h:body>
<h2>Custom Component Example</h2>
<h:form>
<tp:loginComponent
usernameLabel = "Enter User Name: "
usernameValue = "#{userData.name}"
passwordLabel = "Enter Password: "
passwordValue = "#{userData.password}"
loginButtonLabel = "Login"
loginButtonAction = "#{userData.login}" />
</h:form>
</h:body>
</html>
准备好完成所有更改后,让我们像在 JSF - 第一个应用程序章节中所做的那样编译并运行该应用程序。如果您的应用程序一切正常,这将产生以下结果。
