Java與Flex學習筆記(19)—-Flex中的Session管理

來源:互聯網
上載者:User


          Session是Jsp 9大內建對象之一,但是在Flex中卻沒有這一概念。曾經看過一本叫《Flex 公司專屬應用程式開發實戰》書,裡面講述了一點利用Session增加控制許可權的問題。本人也嘗試著做了一個小demo.


           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,代碼如下所示:


<?xmlversion="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,代碼如下所示:


<?xmlversion="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.


          原創文章,轉載請註明出處:http://www.dianfusoft.com/

 



聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.