FLEX中的Session管理
来源:互联网 发布:网络维护企业名录 编辑:程序博客网 时间:2024/05/16 18:38
最近做的项目需要保存从FLEX端登录进来的信息。我最先想到的是HttpServlet的Session来管理。以失败而告终。又试着用全局变量保存,也失败了。折腾的够呛。今天又在苦苦寻找,无意中看到一篇文章,终于解决。非常感谢原文作者!
我的代码因为是实际项目,牵涉点较多,可能影响你观察FLEX中的Session,你可以看原文章,较为简洁。地址:http://blog.csdn.net/zc529739024/article/details/8045227#reply
FLEX中实现Session的一个类是FlexContext类,他将SESSION保存在服务端。
1、首先,确保你的BLAZEDS搭建(将下载得到的blazeds解压,将解压得到的WEB-INF和META-INF两个文件夹拷贝到你的WEB-ROOT文件下)的利用Object 与java通讯正常。
2、新建java类 FlexSession:
package com.greencloud.service;import flex.messaging.FlexContext; public class FlexSession {/** * 保存Session * @author wangfeng * @date 2013-01-23 11:12:39 * @param name * @param value */public void setSession(String name,String value){ FlexContext.getFlexSession().setAttribute(name,value); }/** * 获取Session * @author wangfeng * @date 2013-01-23 11:12:50 * @param name * @return */public String getSession(String name){ String value=(String) FlexContext.getFlexSession().getAttribute(name); return value; } /** * 清除Session * @author wangfeng * @date 2013-01-23 11:13:10 * @param name */ public void removeSession(String name){ FlexContext.getFlexSession().removeAttribute(name); }}
3、在WBE-INF\flex\remoting-config.xml里配置:
<?xml version="1.0" encoding="UTF-8"?><service id="remoting-service" class="flex.messaging.services.RemotingService"> <adapters> <adapter-definition id="java-object" class="flex.messaging.services.remoting.adapters.JavaAdapter" default="true"/> </adapters> <default-channels> <channel ref="my-amf"/> </default-channels> <destination id="serviceTest"><properties><source>test.ServiceTest</source></properties></destination><destination id="UserService"><properties><source>com.greencloud.service.UserService</source></properties></destination> <destination id="dayReportService"><properties><source>com.greencloud.dao.DayreportDao</source></properties></destination><destination id="sessionManage"><properties><source>com.greencloud.service.FlexSession</source></properties></destination></service>
UserService.java:
package com.greencloud.service;import java.sql.Connection;import java.sql.PreparedStatement;import java.sql.ResultSet;import java.sql.SQLException;import java.util.ArrayList;import java.util.List;import javax.servlet.http.HttpServlet;import com.greencloud.db.DBUtlie;import com.greencloud.entity.User;public class UserService extends HttpServlet{/** * */private static final long serialVersionUID = 1L;private Connection con = null;private PreparedStatement ps;private ResultSet rs;private User user; private FlexSession flexSession;public List<User> getUserList()throws SQLException{List<User> list = new ArrayList<User>();try{list = new ArrayList<User>();con = new DBUtlie().getConnection();String sql = "select * from s_user";ps = con.prepareStatement(sql);rs = ps.executeQuery();while(rs.next()){User user = new User();System.out.println("query it's ok");user.setUserId(rs.getInt("userId"));user.setLoginName(rs.getString("loginName"));user.setPassword(rs.getString("password"));user.setRealName(rs.getString("realName"));user.setEmail(rs.getString("email"));user.setDesc(rs.getString("desc"));user.setDeptId(rs.getInt("deptId"));user.setDeptName(rs.getString("deptName"));user.setOrderBy(rs.getInt("orderBy"));user.setIsAdmin(rs.getInt("isAdmin"));user.setIsLocked(rs.getInt("isLocked"));list.add(user);}}catch (Exception e) {e.printStackTrace();}finally{try{rs.close();ps.close();con.close();}catch (Exception e) {e.printStackTrace();}}return list;}public String userLogin(String name,String password){String userName = name;System.out.println("loginName:"+name);System.out.println("password:"+password);List<User> list = new ArrayList<User>();try{list = new ArrayList<User>();con = new DBUtlie().getConnection();String sql = "select * from s_user where loginName = '"+userName+"'";ps = con.prepareStatement(sql);rs = ps.executeQuery();while(rs.next()){User user = new User();System.out.println("query it's ok");user.setUserId(rs.getInt("userId"));user.setLoginName(rs.getString("loginName"));user.setPassword(rs.getString("password"));user.setRealName(rs.getString("realName"));user.setEmail(rs.getString("email"));user.setDesc(rs.getString("desc"));user.setDeptId(rs.getInt("deptId"));user.setDeptName(rs.getString("deptName"));user.setOrderBy(rs.getInt("orderBy"));user.setIsAdmin(rs.getInt("isAdmin"));user.setIsLocked(rs.getInt("isLocked"));list.add(user);}}catch (Exception e) {e.printStackTrace();}finally{try{rs.close();ps.close();con.close();}catch (Exception e) {e.printStackTrace();}}if(list == null || list.size()<1){return "fail";}for(int i=0;i<list.size();++i){user = list.get(i);}System.out.println("password:"+user.getPassword());if(user.getPassword().equals(password)){flexSession = new FlexSession();System.out.println("login success...");flexSession.setSession("account", user.getRealName());System.out.println("flexSession is ok!");return "success";}else{System.out.println("login fail...");return "fail";}}}
FLEX端:
登录页:
logClient.mxml:
<?xml version="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="application1_applicationCompleteHandler(event)"><fx:Declarations><!-- 将非可视元素(例如服务、值对象)放在此处 --><!--<s:RemoteObject id="sessionManage" endpoint="http://localhost:8080/log/messagebroker/amf" destination="sessionManage"><s:method name="setSession" result="sessionManage_resultHandler(event)" fault="sessionManage_faultHandler(event)"/></s:RemoteObject>--><s:RemoteObject id="UserService" endpoint="http://localhost:8080/log/messagebroker/amf" destination="UserService"><s:method name="userLogin" result="userLogin_resultHandler(event)" fault="userLogin_faultHandler(event)"/></s:RemoteObject></fx:Declarations><fx:Script><![CDATA[import com.greencloud.view.IndexView;import com.greencloud.vo.User;import mx.charts.CategoryAxis;import mx.collections.ArrayList;import mx.controls.Alert;import mx.controls.Tree;import mx.core.Application;import mx.events.FlexEvent;import mx.events.Request;import mx.managers.PopUpManager;import mx.rpc.events.FaultEvent;import mx.rpc.events.ResultEvent;//初始化方法 protected function application1_applicationCompleteHandler(event:FlexEvent):void{this.addEventListener(KeyboardEvent.KEY_DOWN,onEnterHandler);}//登录protected function login_clickHandler(event:MouseEvent):void{var strLoginName:String = loginName.text;var strPassword:String = password.text;if(strLoginName!="" && strPassword!=""){UserService.userLogin(strLoginName,strPassword);}else{Alert.show("用户名或密码不能为空","系统提示");}}//监听回车键protected function onEnterHandler(event:KeyboardEvent):void{if(event.keyCode == Keyboard.ENTER){var strLoginName:String = loginName.text;var strPassword:String = password.text;if(strLoginName!="" && strPassword!=""){UserService.userLogin(strLoginName,strPassword);}else{Alert.show("用户名或密码不能为空","系统提示");}}}protected function userLogin_resultHandler(event:ResultEvent):void{var str:String = event.result as String;if(str == "success"){Alert.show("this.user is...");var indexUrl:URLRequest = new URLRequest("http://localhost:8080/log/logClient-debug/IndexView.html");try{navigateToURL(indexUrl,"_self");}catch(e:Error){Alert.show("跳转页面失败","系统提示");}}else{Alert.show("登录失败,请检查用户名和密码的正确性","系统提示");}}protected function userLogin_faultHandler(event:FaultEvent):void{Alert.show(event.fault.message,"出错了"); }//重置protected function reset_clickHandler(event:MouseEvent):void{loginName.text=null;password.text=null;}]]></fx:Script><s:Panel width="100%" height="70%" top="50" bottom="10" left="10" right="10"><s:layout><s:HorizontalLayout paddingBottom="5" paddingLeft="5" paddingRight="5" paddingTop="5" gap="10"/></s:layout><s:VGroup width="100%" height="100%"></s:VGroup><s:VGroup width="100%" height="100%"><s:Spacer width="100%" height="100%"></s:Spacer><mx:FormItemLabel text="用户登录:" fontSize="15" fontWeight="normal"/><s:FormItem label="用户名:" textAlign="left" fontSize="13"><s:HGroup><s:Spacer width="5" height="100%"/><s:TextInput id="loginName" maxChars="250"/></s:HGroup></s:FormItem><s:FormItem label="密码:" textAlign="left" fontSize="13"><s:HGroup><s:Spacer width="17" height="100%"/><s:TextInput id="password" maxChars="250" displayAsPassword="true"/></s:HGroup></s:FormItem><s:HGroup width="100%"><s:Spacer width="15" height="100%"/><s:Button id="login" label="登录" click="login_clickHandler(event)"/><s:Spacer width="35" height="100%"/><s:Button id="reset" label="重置" click="reset_clickHandler(event)"/></s:HGroup><s:Spacer width="100%" height="100%"></s:Spacer></s:VGroup></s:Panel></s:Application>
跳转页:IndexView.mxml
<?xml version="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="application1_applicationCompleteHandler(event)"><fx:Declarations><!-- 将非可视元素(例如服务、值对象)放在此处 --><s:RemoteObject id="sessionManage" destination="sessionManage" endpoint="http://localhost:8080/log/messagebroker/amf"> <s:method name="getSession" result="sessionManage_resultHandler(event)" fault="sessionManage_faultHandler(event)"/></s:RemoteObject></fx:Declarations><fx:Script><![CDATA[import com.greencloud.component.PanelEx;import com.greencloud.vo.User;import mx.collections.ArrayList;import mx.controls.Alert;import mx.controls.Tree;import mx.core.FlexGlobals;import mx.events.FlexEvent;import mx.rpc.events.FaultEvent;import mx.rpc.events.ResultEvent;[Bindable]private var account:String=""; protected function sessionManage_resultHandler(event:ResultEvent):void { //注意获得Session的方式 account=event.result as String; message.text=account+",欢迎您归来..." }protected function sessionManage_faultHandler(event:FaultEvent):void { Alert.show("获取session失败...","提示"); } protected function application1_applicationCompleteHandler(event:FlexEvent):void{//获取session sessionManage.getSession("account"); }protected function exist_clickHandler(event:MouseEvent):void { // TODOAuto-generated method stub //清除session sessionManage.removeSession("account"); var url:URLRequest=new URLRequest("http://localhost:8080/log/logClient-debug/logClient.html"); navigateToURL(url,"_self"); } ]]></fx:Script><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>
- Flex中的Session管理
- FLEX中的Session管理
- Flex中的Session管理
- flex session管理
- Java与Flex学习笔记(19)----Flex中的Session管理
- Flex中的MySQL管理
- Flex中的MySQL管理
- Flex中的MySQL管理
- flex中的mysql管理
- Flex中的MySQL管理
- Hibernate中的Session管理
- tomcat中的session管理
- PHP4中的SESSION管理
- Hibernate中的Session管理
- WebService中的Session管理
- tomcat中的session管理
- Hibernate中的Session管理
- Flex中的MySQL管理(转载)
- 详解Struts1中的struts-config.xml配置文件
- scheme null?返回值
- js获取CKEditor内容
- 【淘宝&SEO】淘宝官方SEO资料
- Tomcat 配置出现的问题
- FLEX中的Session管理
- iphone游戏开发之cocos2d ( 十 )使用纹理图册,更方便生成精灵动画
- linux 进程查看命令
- Jtidy解析脚本时候出现StringIndexOutOfBoundsException异常问题
- Z-STACK之cc2530LED驱动详解
- 利用 GDB Debug Android SurfaceFlinger
- OpenCV 编程入门
- 自己收集的c语言学习网站
- android中用XMPP Asmack获取用户朋友的presence(名单列表)的实现