tapestry session一般有两种方式:persist与sso,persist方法虽然简单,可是过多的使用会增长了服务器的负担,同时其URL也没法被作为书签保存。sso被保存在Session中,能够被同一用户的全部页面共享,但不会被其余用户共享,经过@SessionState标记,作用户登陆使用。源码以下:html
MyPersist.javajava
/**
* 项目名称:TapestryStart
* 开发模式:Maven+Tapestry5.x+Tapestry-hibernate+Mysql
* 版本:1.0
* 编写:飞风
* 时间:2012-02-29
*/
package com.tapestry.app.pages;
import org.apache.tapestry5.annotations.Persist;
import org.apache.tapestry5.annotations.Property;
public class MyPersist {
//直接把hello存储到session
@Property
private String hello;
}
MyPersist.tmlsql
<html t:type="layout" title="tapestryStart Index" t:sidebarTitle="Framework Version"
xmlns:t="http://tapestry.apache.org/schema/tapestry_5_3.xsd" xmlns:p="tapestry:parameter">
您输入的值:${hello}<br/>
<form t:type="form">
<input t:type="textfield" t:id="hello" value="hello"/>
<input type="submit" value="提交"/>
</form>
</html>
Signin.javaapache
/**
* 项目名称:TapestryStart
* 开发模式:Maven+Tapestry5.x+Tapestry-hibernate+Mysql
* 版本:1.0
* 编写:飞风
* 时间:2012-02-29
*/
package com.tapestry.app.pages;
import org.apache.tapestry5.annotations.Property;
import org.apache.tapestry5.annotations.SessionState;
public class Signin {
@Property
private String userName;
@Property
private String password;
@SessionState
private String user;
Object onSuccess(){
if("fly".equals(userName) && "123".equals(password)){
user = userName;
return Welcome.class;
}
return null;
}
}
Signin.tml服务器
<html t:type="layout" title="tapestryStart Index" t:sidebarTitle="Framework Version"
xmlns:t="http://tapestry.apache.org/schema/tapestry_5_3.xsd" xmlns:p="tapestry:parameter">
<t:form>
用户名:<input t:type="textfield" t:id="userName" value="userName"/><br/>
密码:<input t:type="textfield" t:id="password" value="password"/><br/>
<input type="submit" value="登陆"/>
</t:form>
</html>
Welcome.javasession
/**
* 项目名称:TapestryStart
* 开发模式:Maven+Tapestry5.x+Tapestry-hibernate+Mysql
* 版本:1.0
* 编写:飞风
* 时间:2012-02-29
*/
package com.tapestry.app.pages;
import org.apache.tapestry5.annotations.Property;
import org.apache.tapestry5.annotations.SessionState;
public class Welcome {
@Property
@SessionState
private String userName;
Object onLogout(){
userName = null;
return Signin.class;
}
}
Welcome.tmlapp
<html t:type="layout" title="tapestryStart Index" t:sidebarTitle="Framework Version"
xmlns:t="http://tapestry.apache.org/schema/tapestry_5_3.xsd" xmlns:p="tapestry:parameter">
${userName},欢迎您!<t:eventlink t:event="logOut">退出</t:eventlink>
</html>
http://localhost/session/MyPersist
http://localhost/session/Signin