- JSF 教程
- JSF - 首页
- JSF - 概述
- JSF - 环境设置
- JSF - 架构
- JSF - 生命周期
- JSF - 第一个应用程序
- JSF - 托管 Bean
- JSF - 页面导航
- JSF - 基本标签
- JSF - Facelet 标签
- JSF - 转换器标签
- JSF - 验证器标签
- JSF - DataTable
- JSF - 复合组件
- JSF - Ajax
- JSF - 事件处理
- JSF - JDBC 集成
- JSF - Spring 集成
- JSF - 表达式语言
- JSF - 国际化
- JSF 有用资源
- JSF - 快速指南
- JSF - 有用资源
- JSF - 讨论
JSF - ui:param 标签
使用ui:param 标签,我们可以将参数传递给模板文件或包含的文件。
在JSF - 模板标签章节中,我们学习了如何创建和使用模板标签。我们定义了各种部分,如页眉、页脚、内容以及组合所有部分的模板。
现在我们将学习 -
如何将参数传递到模板的各个部分
如何将参数传递到模板
模板部分的参数
创建参数:common.xhtml
将参数添加到 ui:include 标签。使用ui:param标签定义一个包含要传递到 Header 部分的值的参数。
<ui:insert name = "header" >
<ui:include src = "/templates/header.xhtml" >
<ui:param name = "defaultHeader" value = "Default Header" />
</ui:include>
</ui:insert>
使用参数:header.xhtml
<ui:composition>
<h1>#{defaultHeader}</h1>
</ui:composition>
模板参数
创建参数:home.xhtml
将参数添加到 ui:composition 标签。使用ui:param标签定义一个包含要传递到模板的值的参数。
<ui:composition template = "templates/common.xhtml"> <ui:param name = "title" value = "Home" /> </ui:composition>
使用参数:common.xhtml
<h:body>
<h2>#{title}</h2>
</h:body>
示例应用程序
让我们创建一个测试 JSF 应用程序来测试 JSF 中的模板标签。
| 步骤 | 描述 |
|---|---|
| 1 | 在JSF - 模板标签章节中说明的包com.tutorialspoint.test下创建一个名为helloworld的项目。 |
| 2 | 修改src → main → webapp → templates文件夹下的header.xhtml和common.xhtml文件。根据以下说明修改它们。 |
| 3 | 根据以下说明修改home.xhtml。保持其余文件不变。 |
| 4 | 编译并运行应用程序以确保业务逻辑按要求工作。 |
| 5 | 最后,将应用程序构建成 war 文件并将其部署到 Apache Tomcat Web 服务器中。 |
| 6 | 使用以下步骤中说明的适当 URL 启动您的 Web 应用程序。 |
header.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:ui = "http://java.sun.com/jsf/facelets">
<body>
<ui:composition>
<h1>#{defaultHeader}</h1>
</ui:composition>
</body>
</html>
common.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:ui = "http://java.sun.com/jsf/facelets">
<h:head></h:head>
<h:body>
<h2>#{title}</h2>
<div style = "border-width:2px; border-color:green; border-style:solid;">
<ui:insert name = "header" >
<ui:include src = "/templates/header.xhtml" >
<ui:param name = "defaultHeader" value = "Default Header" />
</ui:include>
</ui:insert>
</div>
<br/>
<div style = "border-width:2px; border-color:black; border-style:solid;">
<ui:insert name = "content" >
<ui:include src = "/templates/contents.xhtml" />
</ui:insert>
</div>
<br/>
<div style = "border-width:2px; border-color:red; border-style:solid;">
<ui:insert name = "footer" >
<ui:include src = "/templates/footer.xhtml" />
</ui:insert>
</div>
</h:body>
</html>
home.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:ui = "http://java.sun.com/jsf/facelets">
<h:body>
<ui:composition template = "templates/common.xhtml">
<ui:param name = "title" value = "Home" />
<ui:define name = "content">
<br/><br/>
<h:link value = "Page 1" outcome = "page1" />
<h:link value = "Page 2" outcome = "page2" />
<br/><br/>
</ui:define>
</ui:composition>
</h:body>
</html>
完成所有更改后,让我们像在 JSF - 第一个应用程序章节中所做的那样编译并运行应用程序。如果应用程序一切正常,这将产生以下结果。
jsf_facelets_tags.htm
广告