Apache Tapestry 内置组件
本章通过合适的示例解释了 Tapestry 的内置组件。 Tapestry 支持超过 65 个内置组件。你还可以创建自定义组件。让我们详细介绍一些值得注意的组件。
如果组件
if 组件用于有条件地渲染块。条件由测试参数检查。
创建一个页面 IfSample.java 如下所示:
package com.example.MyFirstApplication.pages; public class Ifsample { public String getUser() { return "user1"; } }
现在,创建一个对应的模板文件如下:
<html t:type = "newlayout" title = "About MyFirstApplication" xmlns:t = "http:// Tapestry.apache.org/schema/tapestry_5_4.xsd" xmlns:p = "tapestry:parameter"> <h3>If-else component example </h3> <t:if test = "user"> Hello ${user} <p:else> <h4> You are not a Tapestry user </h4> </p:else> </t:if> </html>
请求页面将呈现如下所示的结果。
Result : http://localhost:8080/MyFirstApplication/ifsample
除非和委托组件
The 除非组件 与上面讨论的 if 组件正好相反。同时, 委托组件 不会自行进行任何渲染。相反,它通常将标记委托给块元素。除非组件可以使用委托和阻塞来有条件地交换动态内容。
创建一个页面 除非.java 如下。
package com.example.MyFirstApplication.pages; import org.apache.tapestry5.Block; import org.apache.tapestry5.annotations.Property; import org.apache.tapestry5.ioc.annotations.Inject; import org.apache.tapestry5.PersistenceConstants; import org.apache.tapestry5.annotations.Persist; public class Unless { @Property @Persist(PersistenceConstants.FLASH) private String value; @Property private Boolean bool; @Inject Block t, f, n; public Block getCase() { if (bool == Boolean.TRUE ) { return t; } else { return f; } } }
现在,创建一个对应的模板文件如下:
<html t:type = "newlayout" title = "About MyFirstApplication" xmlns:t = "http:// Tapestry.apache.org/schema/tapestry_5_4.xsd" xmlns:p = "tapestry:parameter"> <h4> Delegate component </h4> <div class = "div1"> <t:delegate to = "case"/> </div> <h4> If-Unless component </h4> <div class = "div1"> <t:if test = "bool"> <t:delegate to = "block:t"/> </t:if> <t:unless test = "bool"> <t:delegate to = "block:notT"/> </t:unless> </div> <t:block id = "t"> bool == Boolean.TRUE. </t:block> <t:block id = "notT"> bool = Boolean.FALSE. </t:block> <t:block id = "f"> bool == Boolean.FALSE. </t:block> </html>
请求页面将呈现如下所示的结果。
Result : http://localhost:8080/MyFirstApplication/除非
循环组件
循环组件是循环集合项并为每个值/迭代呈现主体的基本组件。
创建Loop页面如下图:
循环.java
package com.example.MyFirstApplication.pages; import org.apache.tapestry5.annotations.Property; public class Loop { @Property private int i; }
然后,创建对应的模板 Loop.tml
Loop.tml
<html t:type = "newlayout" title = "About MyFirstApplication" xmlns:t = "http:// Tapestry.apache.org/schema/tapestry_5_4.xsd" xmlns:p = "tapestry:parameter"> <p>This is sample parameter rendering example...</p> <ol> <li t:type = "loop" source = "1..5" value = "var:i">${var:i}</li> </ol> </html>
Loop组件有以下两个参数:
-
source : 收藏来源。 1…5 是用于创建具有指定范围的数组的属性扩展。
-
var : 渲染变量。用于呈现模板正文中的当前值。
请求页面会呈现如下结果:
页面链接组件
PageLink 组件用于将页面从一个页面链接到另一个页面。创建一个PageLink测试页面如下: PageLink.java .
package com.example.MyFirstApplication.pages; public class PageLink { }
然后,创建一个对应的模板文件,如下图:
页面链接.tml
<html t:type = "newlayout" title = "About MyFirstApplication" xmlns:t = "http:// Tapestry.apache.org/schema/tapestry_5_4.xsd" xmlns:p = "tapestry:parameter"> <body> <h3><u>Page Link</u> </h3> <div class = "page"> <t:pagelink page = "Index">Click here to navigate Index page</t:pagelink> <br/> </div> </body> </html>
PageLink 组件有一个页面参数,它应该引用目标挂毯页面。
Result : http://localhost:8080/myFirstApplication/pagelink
事件链接组件
EventLink 组件通过 URL 发送事件名称和相应的参数。创建一个 EventsLink 页面类,如下所示。
事件链接.java
package com.example.MyFirstApplication.pages; import org.apache.tapestry5.annotations.Property; public class EventsLink { @Property private int x; void onActivate(int count) { this.x = x; } int onPassivate() { return x; } void onAdd(int value) { x += value; } }
然后,创建一个对应的“EventsLink”模板文件,如下:
事件链接.tml
<html t:type = "newlayout" title = "About MyFirstApplication" xmlns:t = "http:// Tapestry.apache.org/schema/tapestry_5_4.xsd" xmlns:p = "tapestry:parameter"> <h3> Event link example </h3> AddedCount = ${x}. <br/> <t:eventlink t:event = "add" t:context = "literal:1"> Click here to add count </t:eventlink><br/> </html>
EventLink 有以下两个参数:
-
Event : EventLink 组件中要触发的事件的名称。默认情况下,它指向组件的 id。
-
Context : 可选参数。它定义了链接的上下文。
Result : http://localhost:8080/myFirstApplication/EventsLink
单击计数值后,页面将在 URL 中显示事件名称,如以下输出屏幕截图所示。
动作链接组件
ActionLink 组件类似于 EventLink 组件,但它只发送目标组件 id。默认事件名称是 action。
创建一个页面“ActivationLinks.java”,如下图,
激活链接.java
package com.example.MyFirstApplication.pages; import org.apache.tapestry5.annotations.Property; public class ActivationLinks { @Property private int x; void onActivate(int count) { this.x = x; } int onPassivate() { return x; } void onActionFromsub(int value) { x -= value; } }
现在,创建一个对应的模板文件,如下图:
激活链接.tml
<html t:type = "Newlayout" title = "About MyFirstApplication" xmlns:t = "http:// Tapestry.apache.org/schema/tapestry_5_4.xsd" xmlns:p = "tapestry:parameter"> <div class = "div1"> Count = ${count}. <br/> <t:actionlink t:id = "sub" t:context = "literal:1"> Decrement </t:actionlink><br/> </div> </html>
在这里, OnActionFromSub 单击 ActionLink 组件时将调用方法。
Result : http://localhost:8080/myFirstApplication/ActivationsLink
警报组件
警告对话框主要用于向用户发出警告消息。例如,如果输入字段需要一些强制文本,但用户没有提供任何输入,那么作为验证的一部分,你可以使用警告框来发出警告消息。
创建一个页面“警报”,如以下程序所示。
警报.java
package com.example.MyFirstApplication.pages; public class Alerts { public String getUser() { return "user1"; } }
然后,创建对应的模板文件如下:
警报.tml
<html t:type = "Newlayout" title = "About MyFirstApplication" xmlns:t = "http:// Tapestry.apache.org/schema/tapestry_5_4.xsd" xmlns:p = "tapestry:parameter"> <h3>Alerts</h3> <div class = "alert alert-info"> <h5> Welcome ${user} </h5> </div> </html>
警报具有三个严重级别,分别是:
- Info
- Warn
- Error
上面的模板是使用信息警报创建的。它被定义为 警报信息 .你可以根据需要创建其他严重性。
请求页面会产生如下结果:
http://localhost:8080/myFirstApplication/Alerts