Apache Tapestry - 内置组件

本章介绍了Tapestry的内置组件以及合适的示例。 Tapestry支持超过65种内置组件。您还可以创建自定义组件。让我们详细介绍一些值得注意的组件。

如果Component

if组件用于有条件地呈现块。条件由测试参数检查。

创建页面 IfSample.java ,如下所示−

 
package com.example.MyFirstApplication.pages;
公共类Ifsample {
public String getUser(){
return"user1";
}
}

现在,创建一个相应的模板文件,如下所示;

 
< html t:type ="newlayout"title ="关于MyFirstApplication"
xmlns:t ="http://tapestry.apache.org/schema/tapestry_5_4.xsd "
xmlns:p ="tapestry:parameter">
< h3> If-else组件示例< / h3>
< t:if test ="user">
Hello $ {user}
< p:else>
< h4>您不是Tapestry用户< / h4>
< / p:else>
< / t:if>
< / html>

请求页面将呈现如下所示的结果。

结果− http:// localhost:8080 / MyFirstApplication / ifsample

If Component Result

除非和委托组件

除非组件正好与上面讨论的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除非{
@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;
}其他{
返回f;
}
}
}

现在,按以下方式创建相应的模板文件;

 
< html t:type ="newlayout"title ="关于MyFirstApplication"
xmlns:t ="http://tapestry.apache.org/ schema / tapestry_5_4.xsd"
xmlns:p ="tapestry:parameter">
< h4>委托组件< / h4>
< div class ="div1">
< t:委托给="案例"/>
< / div>
< h4> If-除非组件< / h4>
< div class ="div1">
< t:if test ="bool">
< t:委托给="block:t"/>
< / t:if>
< t:除非test ="bool">
< t:委托给="block:notT"/>
< / t:除非>
< / 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>

请求页面将呈现如下所示的结果。

结果− http:// localhost:8080 / MyFirstApplication / unless

委托组件

循环组件

循环组件是循环集合项并为每个值/迭代渲染主体的基本组件。

创建一个循环页面如下所示−

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 ="关于MyFirstApplication"
xmlns:t ="http://tapestry.apache。 org / schema / tapestry_5_4.xsd"
xmlns:p ="tapestry:parameter">
< p>这是示例参数呈现示例...< / p>
< ol>
< li t:type ="loop"source ="1..5"value ="var:i"> $ {var:i}< / li>
< / ol>
< / html>

循环组件具有以下两个参数−

  • 来源−收集来源。 1 ... 5是用于创建具有指定范围的数组的属性扩展。

  • var −渲染变量。用于呈现模板正文中的当前值。

请求页面将呈现如下所示的结果−

循环组件

PageLink组件

PageLink组件用于将页面从一个页面链接到另一个页面。创建一个PageLink测试页面,如下所示; PageLink.java

 
package com.example.MyFirstApplication.pages;
公共类PageLink {
}

然后,创建一个相应的模板文件,如下所示−

PageLink.tml

 
< html t:type ="newlayout"title ="关于MyFirstApplication"
xmlns: t ="http://tapestry.apache.org/schema/tapestry_5_4.xsd"
xmlns:p ="tapestry:parameter">
< body>
< h3>< u>页面链接< / u> < / H3>
< div class ="page">
< t:pagelink page ="索引">点击此处浏览索引页< / t:pagelink>
< br />
< / div>
< / body>
< / html>

PageLink组件有一个页面参数,应引用目标挂毯页面。

结果− http:// localhost:8080 / myFirstApplication / pagelink

Page Link

EventLink组件

EventLink组件通过URL发送事件名称和相应的参数。创建一个EventsLink页面类,如下所示。

EventsLink.java

 
package com.example.MyFirstApplication .PAGES;
import org.apache.tapestry5.annotations.Property;
公共类EventsLink {
@Property
private int x;
void onActivate(int count){
this.x = x;
}
int onPassivate(){
return x;
}
void onAdd(int value){
x + = value;
}
}

然后,按照以下方式创建相应的"EventsLink"模板文件;

EventsLink.tml

 
< html t:type ="newlayout"title ="关于MyFirstApplication"
xmlns:t ="http://tapestry.apache.org/schema/tapestry_5_4.xsd"
xmlns:p ="tapestry:parameter">
< h3>事件链接示例< / h3>
AddedCount = $ {x}。 <峰; br />
< t:eventlink t:event ="add"t:context ="literal:1">
点击此处添加点数
< / t:eventlink>< br />
< / html>

EventLink有以下两个参数−

  • 事件−要在EventLink组件中触发的事件的名称。默认情况下,它指向组件的ID。

  • 上下文−这是一个可选参数。它定义了链接的上下文。

结果− http:// localhost:8080 / myFirstApplication / EventsLink

Event Link

点击计数值后,页面将在URL中显示事件名称,如以下输出屏幕截图所示。

事件链接结果

ActionLink组件

ActionLink组件类似于EventLink组件,但它只发送目标组件ID。默认事件名称是action。

创建一个页面"ActivationLinks.java",如下所示,

ActivationLinks.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;
}
}

现在,创建一个相应的模板文件,如下所示−

ActivationLinks.tml

 
< html t:type ="Newlayout"title ="关于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">
减少
< / t:actionlink>< br />
< / div>
< / html>

此处,单击ActionLink组件时将调用 OnActionFromSub 方法。

结果− http:// localhost:8080 / myFirstApplication / ActivationsLink

Action Link

警报组件

警告对话框主要用于向用户发出警告消息。例如,如果输入字段需要一些必填文本但用户不提供任何输入,那么作为验证的一部分,您可以使用警告框发出警告消息。

创建一个页面"Alerts",如下面的程序所示。

Alerts.java

 
package com .example.MyFirstApplication.pages;
公共类警报{
public String getUser(){
return"user1";
}
}

然后,创建一个相应的模板文件,如下所示;

Alerts.tml

 
< html t:type ="Newlayout"title ="关于MyFirstApplication"
xmlns:t ="http ://tapestry.apache.org/schema/tapestry_5_4.xsd"
xmlns:p ="tapestry:parameter">
< h3>提醒< / h3>
< div class ="alert alert-info">
< h5>欢迎$ {user}< / h5>
< / div>
< / html>

警报有三个严重等级,分别是&;;

  • 信息
  • 警告
  • 错误

以上模板是使用信息提醒创建。它被定义为 alert-info 。您可以根据需要创建其他严重性。

请求页面将产生以下结果−

http:// localhost :8080 / myFirstApplication / Alerts

Alerts