java与flex通信.docx

上传人:b****0 文档编号:9797812 上传时间:2023-05-21 格式:DOCX 页数:12 大小:342.34KB
下载 相关 举报
java与flex通信.docx_第1页
第1页 / 共12页
java与flex通信.docx_第2页
第2页 / 共12页
java与flex通信.docx_第3页
第3页 / 共12页
java与flex通信.docx_第4页
第4页 / 共12页
java与flex通信.docx_第5页
第5页 / 共12页
java与flex通信.docx_第6页
第6页 / 共12页
java与flex通信.docx_第7页
第7页 / 共12页
java与flex通信.docx_第8页
第8页 / 共12页
java与flex通信.docx_第9页
第9页 / 共12页
java与flex通信.docx_第10页
第10页 / 共12页
java与flex通信.docx_第11页
第11页 / 共12页
java与flex通信.docx_第12页
第12页 / 共12页
亲,该文档总共12页,全部预览完了,如果喜欢就下载吧!
下载资源
资源描述

java与flex通信.docx

《java与flex通信.docx》由会员分享,可在线阅读,更多相关《java与flex通信.docx(12页珍藏版)》请在冰点文库上搜索。

java与flex通信.docx

java与flex通信

Java与flex通信有三种方式:

                       1 flex与普通java类通信RemoteObject

                       2flex与服务器交互HTTPService

                       3flex与webservice交互WebService

下面讲RemoteObject和WebService两种方式

一.RemoteObject控件

BlazeDS是一个基于服务器的Java远程调用(remoting)和Web消息传递(messaging)技术,使得运行在浏览器上的Flex应用程序通过使用RemoteObject控件能够实现和后台的Java应用程序相互通信。

Java端

创建一个javaproject例如名为shownewclass创建一个showme类在里面写一个showout方法

publicclassshowme{

publicStringshowout(Stringvalue){

return"show"+value;

}

}

右击shownew—folder名为web(可以随便起)把blaseds中的WEB-INF和WETA-INF放到web中找到web-WEB-INF-flex-remoting-config右击openwith-text-Editor在和之间加入如下代码

showme

destination标签中id可以随便设写flex端时候destination标签中id必须与java端相同source标签中的字段是showme类(而不是其中的方法showout)

右击show-properties-JavaBuilderPath-source修改Defaultoutputfolder为show/web/WEB-INF/classes

Java端的就完成了

在tomcat的安装目录D:

\Tomcat6.0\conf\Catalina\localhost创建一个show.xml文件在里面写入java的路径注意:

路径中不能有中文且是双斜杠

xmlversion="1.0"encoding="UTF-8"?

>

\\DocumentsandSettings\\Administrator\\workspace\\show\\web">

启动tomcat

Flex端

文件-新建-flex项目项目名为play应用程序类型为web应用程序服务器类型为J2EE选择使用远程对象访问服务BlazeDS

点击下一步设置根文件夹根目录和上下根目录根文件夹为java的路径

设置完点验证配置若有效会显示web根文件夹和跟URL有效同时输出文件夹自动修改了

点击完成即可

在play.mxml中粘贴代码

xmlversion="1.0"encoding="utf-8"?

>

Applicationxmlns:

fx="

xmlns:

s="library:

//

xmlns:

mx="library:

//minWidth="955"minHeight="600">

Script>

[CDATA[

importmx.rpc.events.FaultEvent;

importmx.rpc.events.ResultEvent;

protectedfunctionremot_resultHandler(event:

ResultEvent):

void

{

//TODOAuto-generatedmethodstub

//访问成功后执行

Labe1.text=event.result.toString();

}

protectedfunctionremot_faultHandler(event:

FaultEvent):

void

{

//TODOAuto-generatedmethodstub

//访问失败后执行

Labe1.text="访问失败!

";

}

protectedfunctionbutt_clickHandler(event:

MouseEvent):

void

{

//TODOAuto-generatedmethodstub

remot.showout(text1.text);

}

]]>

Script>

Declarations>

--将非可视元素(例如服务、值对象)放在此处-->

RemoteObjectid="remot"destination="myservice"result="remot_resultHandler(event)"fault="remot_faultHandler(event)"/>

Declarations>

Labelid="Labe1"x="237"y="221"text="标签"/>

Buttonid="butt"x="235"y="168"label="say"click="butt_clickHandler(event)"/>

TextInputid="text1"x="235"y="195"/>

Application>

 

若有数据库的项目需要右击项目名--Properties--javaBuildPath--Libraries--AddExternalJARs加入mysql驱动mysql-connector-java-5.1.7-bin.jar还要在tomcat的lib目录下面也加上此驱动。

 

二.WebService控件

Flex中的

WebService>组件专门用于调用和处理WebService。

Java端

下载cxf解压后会看到有一个lib文件夹

右击项目名—properties—javabuilderpath—libraries—addexternaljars将lib文件夹中的所有文件都导入进去

在要发布的服务接口类开头加上@WebService

在接口的实现类开头也加上@WebService

若两个类不在同一个包中则还要用targetNamespace指明目标命名空间如下图

新建一个包lee包里新建一个类mainserver来发布服务

packagelee;

importjavax.xml.ws.Endpoint;

importcom.bao.db.service.ProductService;

importcom.bao.db.service.impl.ProductServiceImpl;

publicclassmainserver{

publicstaticvoidmain(String[]args){

Stringadd="http:

//localhost:

7777/pro";//发布的地址

ProductServiceps=newProductServiceImpl();//要发布的接口的实现类

Endpoint.publish(add,ps);

System.out.println("暴露成功");

}

}

RunasJavaApplication在控制台有如下输出

在浏览器中粘贴发布的地址在末尾加上?

wsdl回车会出现

此时就发布成功了

在flex中可以通过WebService来访问这个地址调用java中的方法例如:

创建一个flexweb项目应用程序服务器类型为无/其他

Declarations>标签中加入如下代码

WebServiceid="ws"wsdl="http:

//localhost:

7777/pro?

wsdl">

operationname="findAllMProduct"result="ws_resultHandlerMProduct(event);"fault="Alert.show(event.fault.faultString,'error');"/>

operationname="deleteByIdProduct"result="ws_Handler(event);"fault="Alert.show(event.fault.faultString,'error');"/>

operationname="updateMProduct"result="ws_Handler(event);"fault="Alert.show(event.fault.faultString,'error');"/>

WebService>

 

operation>标签中name声明java端要被调用的方法名result里面是调用成功后执行的函数fault里面是调用失败执行的函数弹出错误信息的提示框

protectedfunctionws_resultHandler(event:

ResultEvent):

void

{

varstr:

String=(String)(event.result);

xmllist=XMLList(str);

xmllistCollection.source=xmllist;

premydata.source=xmllistCollection.toArray();

initdata(premydata);

}

//ws_resultHandler函数将java端返回的String转换成ArrayCollection

protectedfunctionws_Handler(event:

ResultEvent):

void

{

ws.showList.send();

}

//ws_Handler函数在执行完删除更新后刷新datagrid

最后右击项目名-属性-构建路径-库路径框架链接选择合并到代码中

此时到输出文件夹中打开对应的swf文件就发现可以读取数据库中的数据

两种方法比较:

RemoteObject方法需要用到BlazeDS要修改BlazeDS里的flex-remoting-config文件还要到TOMCAT里创建xml文件告诉flex要访问的java项目的路径flex创建项目时要使用远程对象访问服务并要设置根文件夹根目录和上下根目录根文件夹为java的路径但是在有涉及到数据库的项目时在输出文件夹中打开swf文件不能显示数据(即使选择了合并到代码中也不行)

WebService方法不需要用到BlazeDs也不用在tomcat中创建xml文件flex端创建项目时也不需要使用远程对象访问服务最后输出文件夹中的swf文件可以显示数据但是要在java端的服务类处在运行状态下才行而java端的服务一次只能有一个处于运行状态。

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 小学教育 > 语文

copyright@ 2008-2023 冰点文库 网站版权所有

经营许可证编号:鄂ICP备19020893号-2