blazeds实现flex类映射java类

最近在学习flex与java交互的技术,其中flex可以通过remoteobject的形式利用中间件与java进行桥接,而这个中间件就是blazeds。

flex的actionscript是面向对象的,所以它与java的语言性质是相同的,只是api实现不同而已,所以如果flex通过remoteobject调用java方法的话必须将java的类映射给flex,这样才方便使用。

具体需求是这样的:在java项目中有一个save(User user)方法,该方法实现保存操作,传入的是一个User的javabean。那么在flex端,如果需要调用java的save(User user)方法,就必须通过actionscript创建一个User类,其中的属性和方法都必须跟java中的User相同,这样才能实现交互。

如果一个项目大了,需要映射的类就非常多,为了方便我们映射,blazeds已经帮我们出好了所有工作。

下面通过实例介绍如何使用blazeds实现类映射:

首先下载blazeds项目文件,解压名为blazeds.war的包,找到WEB-INF目录,将lib下的jar文件放入java项目lib目录下,将web.xml的文件拷贝到项目的web.xml下(建议将原web.xml的内容全部注释,待映射完成后再恢复)。

随后找到web.xml中间一段名为“RDSDispatchServlet”的配置,去掉对该段配置的注释,同时将参数“useAppserverSecurity”的值设为false,该段代码就是设置远程数据服务的,实现flex与java的数据交互:


blazeds实现flex类映射java类

随后打开WEB-INF/flex/remoting-config.xml文件,在<service>标签体中添加供flex调用的类配置,具体代码如下:

Xml代码blazeds实现flex类映射java类
  1. <destinationid="LoginServiceImpl">
  2. <properties>
  3. <source>com.bless.login.service.impl.LoginServiceImpl</source>
  4. </properties>
  5. </destination>
  6. <destinationid="LoginInfoServiceImpl">
  7. <properties>
  8. <source>com.bless.ospm.service.impl.LoginInfoServiceImpl</source>
  9. </properties>
  10. </destination>

注意:这段配置就是指引flex如何访问java对象,同时也是指引blazeds配置类映射的功能。另外这里配置的一定是实现类地址,不能是接口。

在com.bless.login.service.impl.LoginServiceImpl类中,我们添加一个方法作为随后的测试:

Java代码blazeds实现flex类映射java类
  1. @Override
  2. publicLoginInfogetLoginInfo(LoginInfolooginInfo){
  3. returnlooginInfo;
  4. }

java端的代码基本完成,我们可以将项目部署到tomcat服务器上,部署成功后打开flash builder。

新建一个flex项目,服务器技术选择j2ee的blazeds:


blazeds实现flex类映射java类

确认无误后选择“下一步”,这一步是关键:“根文件夹”表示java项目的部署地址,根表示通过http访问项目的根路径,上下文根目录表示java项目的根目录名,输出文件夹表示编译flex后生产的flex文件存放地址。


blazeds实现flex类映射java类
输入完成后选择“验证配置”,如果提示有效则选择确定即可。

下面开始配置类映射:flash builder下面工具栏有一个“数据/服务”,选择“连接数据/服务”

blazeds实现flex类映射java类

数据类型选择blazeds,点击“下一步”,如果提示输入身份认证,选择“不需要密码”:


blazeds实现flex类映射java类

随后我们可以看到在表格中已经列出了可连接的服务项,这些服务器项是在java WEB-INF/flex/remoting-config.xml中配置的。全选点击确定即可:

blazeds实现flex类映射java类

注意:你可能会遇到这样的错误“类名 contains overloaded method and is not supported for introspection”意思是类中有方法重载(存在同名方法)无法解析,我目前的办法是把重名方法修改掉,不过这不是万全之策。

生成成功之后,我们可以看到在flex项目下生成了很多as类,这些就是我们需要的:


blazeds实现flex类映射java类

最后编写flex代码测试:

Xml代码blazeds实现flex类映射java类
  1. <?xmlversion="1.0"encoding="utf-8"?>
  2. <s:Applicationxmlns:fx="http://ns.adobe.com/mxml/2009"
  3. xmlns:s="library://ns.adobe.com/flex/spark"
  4. xmlns:mx="library://ns.adobe.com/flex/mx"minWidth="955"minHeight="600"xmlns:services="services.*">
  5. <fx:Script>
  6. <![CDATA[
  7. importmx.controls.Alert;
  8. importmx.rpc.events.FaultEvent;
  9. importmx.rpc.events.ResultEvent;
  10. importvalueObjects.LoginInfo;
  11. //远程请求的回调函数
  12. protectedfunctionresultHandler(event:ResultEvent):void
  13. {
  14. varlogin:LoginInfo=event.resultasLoginInfo;
  15. Alert.show("loginCode:"+login.loginCode+"password:"+login.password);
  16. }
  17. protectedfunctionfaultHandler(event:FaultEvent):void
  18. {
  19. }
  20. protectedfunctionbutton1_clickHandler(event:MouseEvent):void
  21. {
  22. varlogin:LoginInfo=newLoginInfo();
  23. login.loginCode=t1.text;
  24. login.password=t2.text;
  25. remoteObject.getLoginInfo(login);
  26. }
  27. ]]>
  28. </fx:Script>
  29. <fx:Declarations>
  30. <s:RemoteObjectid="remoteObject"
  31. destination="LoginServiceImpl"
  32. result="resultHandler(event)"
  33. fault="faultHandler(event)">
  34. </s:RemoteObject>
  35. </fx:Declarations>
  36. <s:TextInputx="66"y="34"id="t1"/>
  37. <s:TextInputx="66"y="77"id="t2"/>
  38. <s:Buttonx="66"y="118"label="按钮"click="button1_clickHandler(event)"/>
  39. </s:Application>

查看运行效果:


blazeds实现flex类映射java类


http://blessht.iteye.com/blog/1113020