Flex中实现session的一个类是FlexContext类,他将session保存在服务端。
新建一个java类FlexSession.java,里面写了各种存贮、获取、清除Session的方法:
- package com.ldfsoft.util;
- import flex.messaging.FlexContext;
- public class FlexSession {
- /**
- * 保存Session
- * @param name
- * @param value
- */
- public void setSession(String name,String value){
- FlexContext.getFlexSession().setAttribute(name,value);
- }
- /**
- * 获取 Session
- */
- public String getSession(String name){
- String value=(String) FlexContext.getFlexSession().getAttribute(name);
- return value;
- }
- /**
- * 清除Session
- * @param name
- */
- public void removeSession(String name){
- FlexContext.getFlexSession().removeAttribute(name);
- }
- }
然后将此类在flex配置文件中配置一下,这个就不说了。
新建一个mxml文件Login.mxml,代码如下所示:
- <?xmlversionxmlversion="1.0" encoding="utf-8"?>
- <s:Application xmlns:fx="http://ns.adobe.com/mxml/2009"
- xmlns:s="library://ns.adobe.com/flex/spark"
- xmlns:mx="library://ns.adobe.com/flex/mx" minWidth="955" minHeight="600" >
- <fx:Script>
- <![CDATA[
- import flash.net.navigateToURL;
- import mx.controls.Alert;
- import mx.events.FlexEvent;
- import mx.rpc.events.FaultEvent;
- import mx.rpc.events.ResultEvent;
- protected function login_clickHandler(event:MouseEvent):void
- {
- // TODOAuto-generated method stub
- var userName:String=username.text;
- var passWorld:String=passworld.text;
- if(userName!=""&&passWorld!=""){
- //保存Session
- sessionManage.setSession("account",userName);
- }else{
- Alert.show("用户名与密码不能为空!","提示");
- }
- }
- protected function sessionManage_faultHandler(event:FaultEvent):void
- {
- // TODOAuto-generated method stub
- Alert.show(event.fault.message,"出错了");
- }
- protected function sessionManage_resultHandler(event:ResultEvent):void
- {
- // TODOAuto-generated method stub
- forwPage();
- }
- private function forwPage():void{
- var url:URLRequest=new URLRequest("http://localhost:9080/red5Flex/swf/welcome.html");
- navigateToURL(url,"_self");
- }
- ]]>
- </fx:Script>
- <fx:Declarations>
- <!-- Place non-visualelements (e.g., services, value objects) here -->
- <s:RemoteObject id="sessionManage" destination="sessionManage" endpoint="../messagebroker/amf" fault="sessionManage_faultHandler(event)">
- <s:method name="setSession"result="sessionManage_resultHandler(event)" />
- </s:RemoteObject>
- </fx:Declarations>
- <s:Panel x="242" y="107" width="549" height="367" title="请登陆" fontSize="16">
- <s:Label x="99" y="106" text="用户名:"/>
- <s:Label x="100" y="175" text="密码:"/>
- <s:TextInput x="179" y="106" id="username"/>
- <s:TextInput x="180" y="175" id="passworld" displayAsPassword="true" />
- <s:Button x="409" y="130" height="48" id="login" label="登陆"click="login_clickHandler(event)"/>
- </s:Panel>
- </s:Application>
这个文件是一个登陆界面,登陆中将用户信息保存在服务端的Session里。
接着新建一个mxml文件welcome.mxml,代码如下所示:
- <?xmlversionxmlversion="1.0" encoding="utf-8"?>
- <s:Application xmlns:fx="http://ns.adobe.com/mxml/2009"
- xmlns:s="library://ns.adobe.com/flex/spark"
- xmlns:mx="library://ns.adobe.com/flex/mx" minWidth="955" minHeight="600"applicationComplete="app_applicationCompleteHandler(event)">
- <fx:Script>
- <![CDATA[
- import mx.controls.Alert;
- import mx.events.FlexEvent;
- import mx.rpc.events.FaultEvent;
- import mx.rpc.events.ResultEvent;
- [Bindable]private var account:String="";
- protected function app_applicationCompleteHandler(event:FlexEvent):void
- {
- // TODOAuto-generated method stub
- //获取session
- sessionManage.getSession("account");
- }
- protected function sessionManage_resultHandler(event:ResultEvent):void
- {
- //注意获得Session的方式
- account=event.result as String;
- message.text=account+",欢迎您归来..."
- }
- protected function sessionManage_faultHandler(event:FaultEvent):void
- {
- // TODOAuto-generated method stub
- Alert.show("获取session失败...","提示");
- }
- protected function exist_clickHandler(event:MouseEvent):void
- {
- // TODOAuto-generated method stub
- //清除session
- sessionManage.removeSession("account");
- var url:URLRequest=new URLRequest("http://localhost:9080/red5Flex/swf/Login.html");
- navigateToURL(url,"_self");
- }
- ]]>
- </fx:Script>
- <fx:Declarations>
- <!-- Place non-visualelements (e.g., services, value objects) here -->
- <s:RemoteObject id="sessionManage" destination="sessionManage" endpoint="../messagebroker/amf" result="sessionManage_resultHandler(event)"fault="sessionManage_faultHandler(event)" />
- </fx:Declarations>
- <s:Label x="394" y="229" fontSize="18" width="229" height="31" id="message"/>
- <s:Button x="443" y="283" label="退出"id="exist" fontSize="16" click="exist_clickHandler(event)"/>
- </s:Application>
当跳转到这个页面时页面初始化后会从服务器获取账户信息,如果有则在页面上展示。点击“退出”按钮则清除session并跳转到登陆页面。
运行Login.mxml页面,页面如下所示:
输入用户信息,点击“登陆”按钮,跳转到welcome.html页面:
点击“退出”按钮后会跳转到首页,再次运行此页面时用户信息则会为null,或者登陆后关闭浏览器再打开此页面也会获取不到session,这就是session的一大特征吧~
通过合理管理Session.可以减少传递参数的麻烦,可以进行权限管理。尽管如此,工作中还是不提倡多用session.