1、使用FLEX自带SharedObject
做登录等类的身份验证功能,传统的WEB开发基本上都要使用session或是cookit。然而在Flex中并没有提供直接操作cookit或是 session的方法。经过查资料发现,Flex提供了SharedObject这个本地对象。有人把它称为“Flash Cookie ”。还真是形 象啊,呵,不管怎么说,毕竟还是有解决办法的。
SharedObject提供了下面的操作本地对象的方法:
SharedObject.clear() 删除本地共享对象;
SharedObject.flush() 立即把共享对象数据写入本地文件;
SharedObject.getLocal() 创建或连接本地共享对象;
SharedObject.getSize() 取得本地共享对象的指定大小数据。
程序代码
<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" creationComplete="init(event)" borderColor="#FFFFFF" themeColor="#FFFFFF" width="800" height="600" backgroundColor="#FFFFFF" textAlign="center" verticalAlign="middle" horizontalAlign="center" backgroundImage="@Embed(source='lt1.jpg')">
<mx:Script>
<![CDATA[
import mx.controls.Alert;
import mx.events.FlexEvent;
private function init(event:FlexEvent):void {
// 创建一个本地保存数据的对象s
var test:SharedObject = SharedObject.getLocal("testcookie");
test.data.user = "testuser";
test.data.psw = "testpwd";
test.flush(); //提交保存
var test2: SharedObject = SharedObject.getLocal("testcookie");
Alert.show(test2.data.user);
Alert.show(test2.data.psw);
}
]]>
</mx:Script>
</mx:Application>
2、使用JAVASCRIPT实现
flex 中 使用 JavaScript 写 Cookie/**
*写Cookie
*
* name 保存到Cookie中的名称
* text 保存到Cookie中的实际值
* savetime 保存到Cookie中的时间
*/
function writeCookie(name,text,savetime)
{
getUrl("javascript:function writeCookie(name, value, hours) "+
" { "+
" var expire = ''; "+
" if(hours != null) "+
" { " +
" expire = new Date((new Date()).getTime() + hours * 3600000); " +
" expire = '; expires=' + expire.toGMTString();"+
" }" +
" document.cookie = name + '=' + escape(value) + expire;"+
"} "+
" writeCookie('"+name+"','"+text+"',"+savetime+"); " );
}
/**
*读Cookie
* flexname flex 中定义的变量
* name 保存到Cookie中的名称
*
flexname 例子:
function set user_name(txt:String) {
username.text=txt; //可以根据情况保存到对应的变量或域中
}
readCookie("user_name",'name');
*/
function readCookie(flexname,name)
{
getUrl( " Javascript: "+
" var name ='"+name+"' ;"+
" var cookieValue = ''; "+
" var search = name + '='; "+
" if(document.cookie.length > 0) "+
" { "+
" offset = document.cookie.indexOf(search); "+
" if (offset != -1) "+
" { "+
" offset += search.length; "+
" end = document.cookie.indexOf(';', offset); "+
" if (end == -1) end = document.cookie.length; "+
" cookieValue = unescape(document.cookie.substring(offset, end)) "+
" } "+
" } "+
" window.document.getElementsByTagName('object')[0].SetVariable ('"+flexname+"',cookieValue);" );
}
3、使用JSP实现
Cookieapp.jsp 文件
<%@ taglib uri="FlexTagLib" prefix="mm"%>
<SCRIPT LANGUAGE="JavaScript">
<!--
function setCookie(userId) {
document.cookie = "userId="+escape(userId);
}
function getCookie() {
var cookieStr = document.cookie;
var index = cookieStr.indexOf("userId=");
if (index == -1) return null;
index = cookieStr.indexOf("=", index) + 1;
var endstr = cookieStr.indexOf(";", index);
if (endstr == -1) endstr = cookieStr.length;
window.document.flexApp.SetVariable("userIdJS", unescape(cookieStr.substring(index, endstr)));
}
-->
</SCRIPT>
<mm:mxml name="flexApp" source="cookie.mxml"/>
cookie.mxml
<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.macromedia.com/2003/mxml" width="300" height="400">
<mx:Script>
function setCookie() {
srv.send({action: 'set', userId: userId.text});
}
function getCookie() {
srv.send({action: 'get'});
}
function setCookieJS() {
getURL("javascript:setCookie('"+userId.text+"')");
}
function getCookieJS() {
getURL("javascript:getCookie()");
}
// callback function invoked by the getCookie() JavaScript function in cookieapp.jsp
function set userIdJS(userIdJS:String) {
userId.text=userIdJS;
}
</mx:Script>
<mx:HTTPService id="srv" url="cookie.jsp" useProxy="false" method="POST"
result="userId.text=srv.result.userId"/>
<mx:Label text="UserId:"/>
<mx:TextInput id="userId"/>
<mx:Button label="Set Cookie Value Using JSP" width="220" click="setCookie()"/>
<mx:Button label="Set Cookie Value Using JavaScript" click="setCookieJS()"/>
<mx:Button label="Get Cookie Value Using JSP" width="220" click="getCookie()"/>
<mx:Button label="Get Cookie Value Using JavaScript" width="220" click="getCookieJS()"/>
</mx:Application>
做登录等类的身份验证功能,传统的WEB开发基本上都要使用session或是cookit。然而在Flex中并没有提供直接操作cookit或是 session的方法。经过查资料发现,Flex提供了SharedObject这个本地对象。有人把它称为“Flash Cookie ”。还真是形 象啊,呵,不管怎么说,毕竟还是有解决办法的。
SharedObject提供了下面的操作本地对象的方法:
SharedObject.clear() 删除本地共享对象;
SharedObject.flush() 立即把共享对象数据写入本地文件;
SharedObject.getLocal() 创建或连接本地共享对象;
SharedObject.getSize() 取得本地共享对象的指定大小数据。
程序代码
<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" creationComplete="init(event)" borderColor="#FFFFFF" themeColor="#FFFFFF" width="800" height="600" backgroundColor="#FFFFFF" textAlign="center" verticalAlign="middle" horizontalAlign="center" backgroundImage="@Embed(source='lt1.jpg')">
<mx:Script>
<![CDATA[
import mx.controls.Alert;
import mx.events.FlexEvent;
private function init(event:FlexEvent):void {
// 创建一个本地保存数据的对象s
var test:SharedObject = SharedObject.getLocal("testcookie");
test.data.user = "testuser";
test.data.psw = "testpwd";
test.flush(); //提交保存
var test2: SharedObject = SharedObject.getLocal("testcookie");
Alert.show(test2.data.user);
Alert.show(test2.data.psw);
}
]]>
</mx:Script>
</mx:Application>
2、使用JAVASCRIPT实现
flex 中 使用 JavaScript 写 Cookie/**
*写Cookie
*
* name 保存到Cookie中的名称
* text 保存到Cookie中的实际值
* savetime 保存到Cookie中的时间
*/
function writeCookie(name,text,savetime)
{
getUrl("javascript:function writeCookie(name, value, hours) "+
" { "+
" var expire = ''; "+
" if(hours != null) "+
" { " +
" expire = new Date((new Date()).getTime() + hours * 3600000); " +
" expire = '; expires=' + expire.toGMTString();"+
" }" +
" document.cookie = name + '=' + escape(value) + expire;"+
"} "+
" writeCookie('"+name+"','"+text+"',"+savetime+"); " );
}
/**
*读Cookie
* flexname flex 中定义的变量
* name 保存到Cookie中的名称
*
flexname 例子:
function set user_name(txt:String) {
username.text=txt; //可以根据情况保存到对应的变量或域中
}
readCookie("user_name",'name');
*/
function readCookie(flexname,name)
{
getUrl( " Javascript: "+
" var name ='"+name+"' ;"+
" var cookieValue = ''; "+
" var search = name + '='; "+
" if(document.cookie.length > 0) "+
" { "+
" offset = document.cookie.indexOf(search); "+
" if (offset != -1) "+
" { "+
" offset += search.length; "+
" end = document.cookie.indexOf(';', offset); "+
" if (end == -1) end = document.cookie.length; "+
" cookieValue = unescape(document.cookie.substring(offset, end)) "+
" } "+
" } "+
" window.document.getElementsByTagName('object')[0].SetVariable ('"+flexname+"',cookieValue);" );
}
3、使用JSP实现
Cookieapp.jsp 文件
<%@ taglib uri="FlexTagLib" prefix="mm"%>
<SCRIPT LANGUAGE="JavaScript">
<!--
function setCookie(userId) {
document.cookie = "userId="+escape(userId);
}
function getCookie() {
var cookieStr = document.cookie;
var index = cookieStr.indexOf("userId=");
if (index == -1) return null;
index = cookieStr.indexOf("=", index) + 1;
var endstr = cookieStr.indexOf(";", index);
if (endstr == -1) endstr = cookieStr.length;
window.document.flexApp.SetVariable("userIdJS", unescape(cookieStr.substring(index, endstr)));
}
-->
</SCRIPT>
<mm:mxml name="flexApp" source="cookie.mxml"/>
cookie.mxml
<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.macromedia.com/2003/mxml" width="300" height="400">
<mx:Script>
function setCookie() {
srv.send({action: 'set', userId: userId.text});
}
function getCookie() {
srv.send({action: 'get'});
}
function setCookieJS() {
getURL("javascript:setCookie('"+userId.text+"')");
}
function getCookieJS() {
getURL("javascript:getCookie()");
}
// callback function invoked by the getCookie() JavaScript function in cookieapp.jsp
function set userIdJS(userIdJS:String) {
userId.text=userIdJS;
}
</mx:Script>
<mx:HTTPService id="srv" url="cookie.jsp" useProxy="false" method="POST"
result="userId.text=srv.result.userId"/>
<mx:Label text="UserId:"/>
<mx:TextInput id="userId"/>
<mx:Button label="Set Cookie Value Using JSP" width="220" click="setCookie()"/>
<mx:Button label="Set Cookie Value Using JavaScript" click="setCookieJS()"/>
<mx:Button label="Get Cookie Value Using JSP" width="220" click="getCookie()"/>
<mx:Button label="Get Cookie Value Using JavaScript" width="220" click="getCookieJS()"/>
</mx:Application>