tapestry的SessionState的研究小结

有关 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 具体解析过程我还是有些说不清楚,待我继续研究。

 

   作者:龙映安

   所属公司: 云软网络

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
提供的源码资源涵盖了小程序应用等多个领域,每个领域都包含了丰富的实例和项目。这些源码都是基于各自平台的最新技术和标准编写,确保了在对应环境下能够无缝运行。同时,源码中配备了详细的注释和文档,帮助用户快速理解代码结构和实现逻辑。 适用人群: 适合毕业设计、课程设计作业。这些源码资源特别适合大学生群体。无论你是计算机相关专业的学生,还是对其他领域编程感兴趣的学生,这些资源都能为你提供宝贵的学习和实践机会。通过学习和运行这些源码,你可以掌握各平台开发的基础知识,提升编程能力和项目实战经验。 使用场景及目标: 在学习阶段,你可以利用这些源码资源进行课程实践、课外项目或毕业设计。通过分析和运行源码,你将深入了解各平台开发的技术细节和最佳实践,逐步培养起自己的项目开发和问题解决能力。此外,在求职或创业过程中,具备跨平台开发能力的大学生将更具竞争力。 其他说明: 为了确保源码资源的可运行性和易用性,特别注意了以下几点:首先,每份源码都提供了详细的运行环境和依赖说明,确保用户能够轻松搭建起开发环境;其次,源码中的注释和文档都非常完善,方便用户快速上手和理解代码;最后,我会定期更新这些源码资源,以适应各平台技术的最新发展和市场需求。 所有源码均经过严格测试,可以直接运行,可以放心下载使用。有任何使用问题欢迎随时与博主沟通,第一时间进行解答!

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值