有关 tapestry 的 @SessionState 的研究过程总结
如果再 tapestry 中想实现页面数据共享,那么可以采用 SSO , SSO 主要用 @SessionState 进行描述 ,下面我将做一个 例子 , 实现过程就是在 login 页面输入用户名和密码登陆之后跳转到 welcome 页面:
页面类: Login.java
import org.apache.tapestry5.annotations.Property;
import org.apache.tapestry5.annotations.SessionState;
public class Login {
@Property
private String userName ;
@Property
private String password ;
// @SessionState 表示 SSO 数据共享
@SessionState
private String user ;
Class<?> onSuccess(){
System. out .println( " 进入了类 Login 的 onSuccess " );
if ( "abc" .equals( userName )&& "123" .equals( password )) {
this . user = userName ;
return Welcome. class ;
}
return null ;
}
}
Login.tml:
< html xmlns:t = "http://tapestry.apache.org/schema/tapestry_5_1_0.xsd" >
< p >
< t:form t:id = "loginForm" >
< div >
用户名: < t:textfield value = "userName" ></ t:textfield >
</ div >
< div >
密 ** 码: < t:passwordfield value = "password" ></ t:passwordfield >
</ div >
< input type = "submit" value = " 登 录 " ></ input >
</ t:form >
</ p >
</ html >
Welcome.java:
import org.apache.tapestry5.annotations.SessionState;
public class Welcome {
@SessionState
private String theUser ;
public String getTheUser() {
return theUser ;
}
}
Welcom.tml:
< html xmlns:t = "http://tapestry.apache.org/schema/tapestry_5_1_0.xsd"
xmlns:p = "tapestry:parameter" >
欢迎 ${theUser}!
</ html >
功能我们已经实现,接下来我们实现退出功能,我们思考一下 退出肯定是要把 共享的数据 清空。那么 我们怎么清空了?我们一起来看看 API 里面的说明:
An SSO field may have a companion field, of type boolean, used to see if the SSO has been created yet. If another field exists with the same name, suffixed with "Exists" (i.e., "sso" for the SSO field, and "ssoExists" for the companion field) and the type of that field is boolean, then access to the field will determine whether the SSO has already been created. This is necessary because even a null check ("sso != null") may force the SSO to be created. Instead, check the companion boolean field ("asoExists").
解释:在 SSO 中有一个可能有一个组合字段,它的类型为 boolean ,用它可以查看 SSO 是否被创建。如果在其它存在字段名字中,有以 ”Exists” 为后缀的 (eg: 如果有一个 SSO 字段为 ”sso” ,那么组合字段名字就是 ”ssoExists”) 并且它的类型为 boolean ,当访问它的时候将确定 SSO 是否被创建。这是因为如果 sso ! =null 表示 SSO 已经被创建。
即可以用 xxxExists 来检测 SSO 是否被创建,还可以得出就是 当 sso 被赋值为 null 表示被清空了。由此我们得出 在退出的时候我们可以 把 SSO 描述的字段赋值为 null 。我们添加方法:
void onActionFromLogout(){
this . theUser = null ;
}
页面修改为:
< html xmlns:t = "http://tapestry.apache.org/schema/tapestry_5_1_0.xsd"
xmlns:p = "tapestry:parameter" >
欢迎 ${theUser}!
< t:actionlink t:id = "logout" > 退出 </ t:actionlink >
</ html >
可能让大家遗憾的是,报错了。说什么 ApplicationStateManager 没有实现什么接口,其实原因就是 因为 SSO 没有被创建,但是我们又要从中去取值而导致的。 现在我们要做的就是在取值的事实先检测 SSO 是否被创建。 大家回过去看下我刚才给大家看的 API ,之后看看解说。
由此我们知道我们需要在页面定义个 private Boolean theUserExists ;, 为了能在页面可以使用我们设置为只读的属性:
在页面类添加
private boolean theUserExists;
public boolean isTheUserExists(){
return this . theUserExists ;
}
页面改为:
< html xmlns:t = "http://tapestry.apache.org/schema/tapestry_5_1_0.xsd"
xmlns:p = "tapestry:parameter" >
< t:if test = "theUserExists" >
欢迎 ${theUser}!
< t:actionlink t:id = "logout" > 退出 </ t:actionlink >
< p:else >
< t:pagelink page = "Login" > 登录 </ t:pagelink >
</ p:else >
</ t:if >
</ html >
Ok, 到这里我们研究结束。因为我也才刚刚进入对 tapestry 的初学过程。完全告 API 进行学习和研究。 Tapestry 的中文文档很少,提供出来希望可以帮助到大家。但是对于 SSO 具体解析过程我还是有些说不清楚,待我继续研究。
作者:龙映安
所属公司: 云软网络