第十一講:tapestry session存儲

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
相關文章
相關標籤/搜索