tapestry5.0之页面组件定义/页间传值/Application State Object

一、页面组件定义方式
1.在模板(tml)文件中明确定义
Html代码 复制代码
  1. <t:textfield t:value="message"/>  
<t:textfield t:value="message"/>

这种方式不能在Dreamweaver中预览。
2.在模板文件中隐示定义
Html代码 复制代码
  1. <input type="text" t:type="textfield" t:value="message"/>  
  2. <a href="#" t:type="PageLink" t:page="Another">Go to Another page</a>  
<input type="text" t:type="textfield" t:value="message"/>
<a href="#" t:type="PageLink" t:page="Another">Go to Another page</a>

3.在页面类中定义
(1)tml中为:
Html代码 复制代码
  1. <a href="#" t:id="theLink">Go to Another page</a>  
  2. <input type="text" t:id="theTextBox"/>  
<a href="#" t:id="theLink">Go to Another page</a>
<input type="text" t:id="theTextBox"/>

(2)在类文件中用annotations来明确组件
Java代码 复制代码
  1. @Component(parameters = {"page=Another"})   
  2. private PageLink theLink;   
  3.   
  4. @Component(parameters = {"value=message"})   
  5. private TextField theTextBox;  
@Component(parameters = {"page=Another"})
private PageLink theLink;

@Component(parameters = {"value=message"})
private TextField theTextBox;


二、页面间传值
假定要将一个字串传给Another页面,Another定义如下:
Java代码 复制代码
  1. public class Another{   
  2. private String passedMessage;   
  3. public String getPassedMessage(){   
  4.    return passedMessage;   
  5. }   
  6. public void setPassedMessage(String passedMessage){   
  7.    this.passedMessage = passedMessage;   
  8. }   
  9. }  
public class Another{
private String passedMessage;
public String getPassedMessage(){
   return passedMessage;
}
public void setPassedMessage(String passedMessage){
   this.passedMessage = passedMessage;
}
}

1.简单传值方式
Java代码 复制代码
  1. @InjectPage  
  2. private Another another;   
  3.   
  4. @OnEvent(value="submit", component="userInputForm")   
  5. Object onFormSubmit(){   
  6.   another.setPassedMessage(message);   
  7.   return another;   
  8. }  
@InjectPage
private Another another;

@OnEvent(value="submit", component="userInputForm")
Object onFormSubmit(){
  another.setPassedMessage(message);
  return another;
}

运行正常,但当浏览器重新请求页面时,传递的数据将丢失。因为Tapestry页面池在更新Another实例时,它不知道你有数据要传到这个页面。
2.将属性域持久化
Java代码 复制代码
  1. @Persist  
  2. private String passedMessage;  
@Persist
private String passedMessage;

这种方式最简单,它将把我们传给persistent property中的值,存储在HttpSession中。这种方式有副作用,一是大量用户同时访问时占内存,二是 Session到期失效。另外每次请求这个页面,Tapestry都会put the value stored for it in the session into that property,no matter what instance of the page
is being used。
3.使用page activation context
这是 Tapestry5专门定义的页面传值方式。增加方法onActivate和onPassivate,它们将在Http请求生命期中的一个适当时候被Tapestry调用。
Java代码 复制代码
  1. /*The onActivate method is invoked every time the page is loaded  
  2.  *it might be to check whether the user who tries to access the page  *was successfully authenticated  
  3. */  
  4. void onActivate(String message){   
  5.   System.out.println("Another page is activated! The message is: " + message);   
  6.   this.passedMessage = message;   
  7. }   
  8.   
  9. String onPassivate(){   
  10.   System.out.println("Another page is passivated...");   
  11.   return passedMessage;   
  12. }  
/*The onActivate method is invoked every time the page is loaded
 *it might be to check whether the user who tries to access the page  *was successfully authenticated
*/
void onActivate(String message){
  System.out.println("Another page is activated! The message is: " + message);
  this.passedMessage = message;
}

String onPassivate(){
  System.out.println("Another page is passivated...");
  return passedMessage;
}

在第一个页面实例放入到页面池之前,Tapesty将检查页面是否使用了activation context(the activation context by implementing the pair of methods, onActivate and onPassivate),如果用了,就调用页面实例的onPassivate方法。
Tapestry将记住onPassivate的返回值,并立刻使用页面的另一个新实例,将记录下的值作为参数传递给新实例的onActivate方法。这样就达到了传值目的。
这种方式不用将值存在 Session中,而是记录在URL中的。如:http://localhost:8080/t5first/another/hi+there%21
(The message was recorded into the URL while redirecting the user's browser)
三、Application State Object
在Tapestry中,应用程序的每个页面可以使用的对象被称为Application State Object (ASO)。通常,这些是有特殊使用目的的数据对象。假如定义了一个User类:
Java代码 复制代码
  1. public class User{   
  2.   public String getFirstName(){   
  3.     return firstName;   
  4.   }   
  5.   public void setFirstName(String firstName){   
  6.     this.firstName = firstName;   
  7.   }   
  8.   public String getLastName(){   
  9.     return lastName;   
  10.   }   
  11.   public void setLastName(String lastName){   
  12.     this.lastName = lastName;   
  13.   }   
  14. }  
public class User{
  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;
  }
}

1.create a private field of the type User and mark it with the @ApplicationState annotation,如在登录页面定义后,在成功登录时写入相应的人员信息。
Java代码 复制代码
  1. @ApplicationState  
  2. private User user;  
@ApplicationState
private User user;

Tapestry将在第一次请求ASO时,创建此ASO的实例。也就是说无论什么时候请求ASO,他将一直存在,不为空。
默认情况下,Application State Objects 存储在 Session中。这就意味着,如果 session还不存在,那么当第一次请求ASO时,它才会被创建。
2.在页面中使用
各个页面可以定义不同的private域,它们都指向同一个Application State Object。
Java代码 复制代码
  1. @ApplicationState  
  2. private User currentUser;   
  3.   
  4. public User getCurrentUser(){   
  5.   return currentUser;   
  6. }  
@ApplicationState
private User currentUser;

public User getCurrentUser(){
  return currentUser;
}

Html代码 复制代码
  1. <p>The user is ${currentUser.firstName} ${currentUser.lastName}</p>  
<p>The user is ${currentUser.firstName} ${currentUser.lastName}</p>

3.检查ASO是否已经存在
定义一个boolean类型的ASO域,它的名称为ASO域加Exists组成。一旦有请求ASO,它将被Tapestry实例化。这个boolean域被置为true,在这之前为false。以user为例:
Java代码 复制代码
  1. private boolean userExists;   
  2. public boolean getUserExists(){   
  3.   return userExists;   
  4. }  
private boolean userExists;
public boolean getUserExists(){
  return userExists;
}

Html代码 复制代码
  1. <t:if t:test="userExists">  
  2. <p>The user is ${user.firstName} ${user.lastName}</p>  
  3. </t:if>  
<t:if t:test="userExists">
<p>The user is ${user.firstName} ${user.lastName}</p>
</t:if>


四、Prefixes: prop and literal
a component's properties can have either a prop or literal default prefix. Say, the label property of the TextField component has the default prefix literal, while the value property of the same component has the default prefix prop.
t:label="User Name"与t:label="literal:User Name"等价。
t:label="prop:theLabel"
In this case Tapestry will look for the getTheLabel method in the page class and use whatever that method returns as the label.
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值