1.使用MyEclipse+Flex插件整合Java&Flex的方法:
https://docs.google.com/fileview?id=0B5pwJS1Mq71jMzM5ZDUwOGMtN2FjNS00Njc2LTlkMjItOTY4ZDU5MDVmNDIx&hl=en
我使用的是第一种。
2.搭建SSH框架
不多说,详见整个工程:
http://other.sotee.com/2010-03-29/100253.html
3.完成User的ORM——User.java,User.hbm.xml
4.设计用于登录的LoginService
Login类:
1 package com.linying.service.login;
2
3 import java.util.List;
4
5 import com.linying.dao.CommonDao;
6
7
8 /**
9 * LoginService,含有用于Flex调用的login方法
10 * @author Ying-er
11 * @Email melody.crazycoding@gmail.com
12 * @time Mar 29, 2010 8:51:32 AM
13 * @version 1.00
14 */
15 public class Login {
16
17 private String name;
18 private String password;
19 /**
20 * Dao的公用接口
21 */
22 private CommonDao commonDao;
23
24 public CommonDao getCommonDao() {
25 return commonDao;
26 }
27
28 public void setCommonDao(CommonDao commonDao) {
29 this.commonDao = commonDao;
30 }
31 public String getName() {
32 return name;
33 }
34
35 public void setName(String name) {
36 this.name = name;
37 }
38
39 public String getPassword() {
40 return password;
41 }
42
43 public void setPassword(String password) {
44 this.password = password;
45 }
46
47 public Login() {
48 // TODO Auto-generated constructor stub
49 }
50
51 public int login(){
52 return login(name, password);
53 }
54
55 /**
56 * 用户名、密码正确返回0,否则返回-1.
57 * @param name
58 * @param password
59 * @return
60 */
61 public int login(String name,String password){
62 System.out.println("---------------------------------->"+name);
63 System.out.println("---------------------------------->"+password);
64 if(name==null||name.trim().equals("")||password==null||password.trim().equals("")){
65 return -1;
66 }
67 String hql="select id from User where loginname = ? and password = ?";
68 String[] values=new String[2];
69 values[0]=name;
70 values[1]=password;
71 List list=this.getCommonDao().getDataListHql(hql, values);
72 if(list==null||list.isEmpty()){
73 return -1;
74 }else{
75 return 0;
76 }
77 }
78
79 }
80
5.配置applicationContext.xml文件
我的文件配置如下:
1 <?xml version="1.0" encoding="UTF-8"?>
2 <beans xmlns="http://www.springframework.org/schema/beans"
3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4 xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
5 <!--===================================================================-->
6 <!--c3p0 dataSource-->
7 <!--===================================================================-->
8 <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close">
9 <property name="driverClass">
10 <value>com.mysql.jdbc.Driver</value>
11 </property>
12 <property name="jdbcUrl">
13 <value>jdbc:mysql://localhost:3306/flexLoginDemo?useUnicode=true&characterEncoding=UTF-8&autoReconnect=true</value>
14 </property>
15 <property name="properties">
16 <props>
17 <prop key="user">root</prop>
18 <prop key="password">1234</prop>
19 <prop key="hibernate.c3p0.acquire_increment">2</prop>
20 <prop key="hibernate.c3p0.idle_test_period">3000</prop>
21 <prop key="hibernate.c3p0.timeout">5000</prop>
22 <prop key="hibernate.c3p0.max_size">800</prop>
23 <prop key="hibernate.c3p0.min_size">1</prop>
24 <prop key="hibernate.c3p0.max_statements">800</prop>
25 <prop key="hibernate.c3p0.validate">false</prop>
26 <prop key="c3p0.testConnectionOnCheckout">true</prop>
27 </props>
28 </property>
29 </bean>
30 <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
31 <property name="dataSource" ref="dataSource" />
32 <property name="mappingResources">
33 <list>
34 <value>/com/linying/domain/User.hbm.xml</value>
35 </list>
36 </property>
37 <property name="hibernateProperties">
38 <value>
39 hibernate.dialect=com.linying.beans.MySQLDialect
40 hibernate.show_sql=true
41 hibernate.generate_statistics=true
42 hibernate.transaction.flush_before_completion=true
43 hibernate.transaction.auto_close_session=true
44 hibernate.autoReconnect=true
45 </value>
46 </property>
47 </bean>
48 <bean id="jdbcExceptionTranslator"
49 class="org.springframework.jdbc.support.SQLErrorCodeSQLExceptionTranslator">
50 <property name="dataSource">
51 <ref bean="dataSource" />
52 </property>
53 </bean>
54 <bean id="hibernateTemplate"
55 class="org.springframework.orm.hibernate3.HibernateTemplate">
56 <property name="sessionFactory">
57 <ref bean="sessionFactory" />
58 </property>
59 <property name="jdbcExceptionTranslator">
60 <ref bean="jdbcExceptionTranslator" />
61 </property>
62 </bean>
63 <bean id="transactionManager"
64 class="org.springframework.orm.hibernate3.HibernateTransactionManager">
65 <property name="sessionFactory" ref="sessionFactory" />
66 <property name="jdbcExceptionTranslator">
67 <ref bean="jdbcExceptionTranslator" />
68 </property>
69 </bean>
70 <bean id="transactionTemplate"
71 class="org.springframework.transaction.support.TransactionTemplate">
72 <property name="transactionManager" ref="transactionManager" />
73 </bean>
74 <!--DAO================================================================-->
75 <bean id="commonDao" class="com.linying.dao.CommonDaoImpl">
76 <property name="hibernateTemplate" ref="hibernateTemplate"/>
77 <property name="transactionTemplate" ref="transactionTemplate"/>
78 </bean>
79 <!--beans================================================================-->
80 <bean id="login" class="com.linying.service.login.Login">
81 <property name="commonDao" ref="commonDao"/>
82 </bean>
83 </beans>
6.在web-INF-->flex-->service-config.xml文件中加入如下结点:
1 <factories>
2 <factory id="SpringFactory" class="com.linying.beans.SpringFactory"/>
3 </factories>
7.在web-inf-->flex-->remoting-config.xml文件中加入如下结点:
<destination id="login">
<properties>
<factory>SpringFactory</factory>
<source>login</source>
</properties>
</destination>
8.在Flex的主程序中,(本工程为flexLoginDemo.mxml)
增加如下结点:
1 <mx:RemoteObject id="login" destination="login" endpoint="messagebroker/amf">
2 <mx:method name="login" fault="faultHandler(event)" result="login_resultHandler(event)"/>
3 </mx:RemoteObject>
其中,“faultHandler”和“login_resultHandler”是自定义as方法
整个主程序代码如下:
1 <?xml version="1.0" encoding="utf-8"?>
2 <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" backgroundGradientAlphas="[1.0, 1.0]" backgroundGradientColors="[#64F333, #F9F10C]" color="#FB14ED" xmlns:ns1="components.*" creationComplete="init();">
3
4 <mx:RemoteObject id="login" destination="login" endpoint="messagebroker/amf">
5 <mx:method name="login" fault="faultHandler(event)" result="login_resultHandler(event)"/>
6 </mx:RemoteObject>
7
8 <mx:Script>
9 <![CDATA[
10 import mx.rpc.events.ResultEvent;
11 import mx.controls.Alert;
12 import mx.rpc.events.FaultEvent;
13 import mx.core.IFlexDisplayObject;
14 import components.loginComponent;
15 import components.loginSucceed;
16 import mx.managers.PopUpManager;
17
18 public var lpobj:IFlexDisplayObject;
19 public var lp:loginComponent=loginComponent(lpobj);
20
21 public function loginButtonDown(event:Event):void{
22 login.login(loginComponent(lpobj).userName.text,loginComponent(lpobj).password.text);
23 }
24
25 public function init():void{
26
27 lpobj=PopUpManager.createPopUp(this,loginComponent,true);
28 PopUpManager.centerPopUp(lpobj);
29 loginComponent(lpobj).loginButton.addEventListener(MouseEvent.CLICK,loginButtonDown);
30
31 }
32
33 public var succeedView:loginSucceed=loginSucceed(lpobj);
34
35 private function login_resultHandler(event:ResultEvent):void{
36 var result:int=event.result as int;
37 if(result==0){
38
39 PopUpManager.removePopUp(lpobj);
40 currentState="index";
41
42
43 }else{
44 Alert.show("用户或者密码不对","登录失败");
45 }
46 }
47
48 private function faultHandler(event:FaultEvent):void {
49 Alert.show(event.fault.toString(), "失败");
50 }
51
52
53 ]]>
54 </mx:Script>
55
56 <mx:states>
57 <mx:State name="index">
58 <mx:AddChild position="lastChild">
59 <ns1:loginSucceed horizontalCenter="20" verticalCenter="-28">
60 </ns1:loginSucceed>
61 </mx:AddChild>
62 </mx:State>
63 </mx:states>
64
65 </mx:Application>
66
9.运行效果截图:
posted on 2010-03-29 13:39
Ying-er 阅读(1424)
评论(5) 编辑 收藏 所属分类:
Flex3.0 、
SSH