还是以那个登录例子来说,登录请求在blazeDS来看不是直接发送URL串来请求,而是通过AS代码与web-info下的
remoting-config.xml配合使用
mxml中重要代码片段
<span style="font-size: medium;"><fx:Declarations> <!-- 将非可视元素(例如服务、值对象)放在此处 --> <s:RemoteObject id="myFlex" destination="mytest" result="myFlex_resultHandler(event)" /></fx:Declarations> </span>
那么这个destination所指的名字就是remoting-config.xml中配置好的名称,具体内容是
<span style="font-size: medium;"><?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="mytest" >
<properties>
<source>com.imgold.test.Login</source>
</properties>
</destination>
</service></span>
xml代码
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 |
<span style="font-size: medium;"><?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="mytest"
> <properties> <source>com.imgold.test.Login</source> </properties> </destination></service></span> |
mxml中光有这个还不行,既然是登陆,肯定有点击发送及处理结果的方法
下面来看下程序的点击登录按钮处理方法
|
1
2
3
4 |
<span style="font-size: medium;">protected
function loginBtn_clickHandler(event:MouseEvent):void{myFlex.getUser(userName.text,passWord.text); }</span> |
这个getUser方法可是JAVA后台处理类程序中的具体方法
上面remoteObject那个result方法是处理程序返回结果的
|
1
2
3
4
5
6
7
8
9
10 |
<span style="font-size: medium;">protected
function myFlex_resultHandler(event:ResultEvent):void{ str=event.result as
String; //Alert.show(str); if(str==‘success‘){ currentState=‘mainState‘; }else{ shake.play(); }}</span> |
其他MXML代码上次发的那个登录示例中有
最后就看下程序处理类的具体代码
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 |
<span style="font-size: medium;">public
class Login { public
String getUser(String
userName,String
passWord){ ApplicationContext context = new
ClassPathXmlApplicationContext("applicationContext.xml"); UserServiceImpl sf = (UserServiceImpl) context.getBean("userService"); User user=sf.login(userName, passWord); System.out.println(user); if(user!=null){ return
"success"; }else{ return
"fail"; } } }</span> |
大功告成,blazeDS最基本的示例可以运行了
原文:http://www.cnblogs.com/regalys168/p/3626882.html