第一次写博客,呵呵,大家见笑了。
这两天非常痛苦的情况下,终于把BlazeDS这个东西配出来了。这里把过程写下来,做个总结,也方便下次忘了,拿出来看吧。
首先介绍一下我的环境:jdk1.6 , eclipse , flashBuilder
1、下载BlazeDS 和tomcat,这个我就不说了,google一下都出来了
2、把下载来的BlazeDS包解压,把里面的BlazeDS.war文件放到tomcat的webapps目录下,启动tomcat,tomcat会自动解压BlazeDS.war文件,出现BlazeDS文件夹,这个就是我们下面要用的东西
3、新建java web工程,将BlazeDS文件夹里面的两个文件文件复制到java工程中替换里面的META-INF,以及WEB-INF文件夹,并且制定工程源代码的输出目录为WEB-INF下的classes文件夹(方法:右击项目-properies-java build path 选择source标签 最下面的文本框内输入路径,点确定就可以了)
4、新建一个flex project ,输入项目名称,直接finish就行了,然后在项目上右击 properise - flex server ,在Server technoloy 中选择j2ee以及选择 blazeDS 一项 。Server location 的配置,这个是难点,root folder :我选择的是java项目中的WebContent目录(绝对路径) Root URL:http://localhost:8080 Context root:/java项目的名称 。我也不知道这么配是个什么意思,但是这么配成功了,以后有机会再研究这里面究竟是什么意思吧。 然后最下面就是你的flash文件最后输出的路径,这个我选择的是java项目的WebContent路径,就是把flash当成java项目的jsp页面一样,然后就可以了。
下面是java代码:
package com.blazeDS.test; public class HelloWorld { public String getInfo() { return "HelloWorld!"; } public String login(String username,String password) { if(username.equals("tiger") && password.equals("123")) { return "登录成功!"; } return "登录失败"; } }
下面是flex的代码:
<?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"> <fx:Script> <!--[CDATA[ import mx.controls.Alert; import mx.rpc.events.FaultEvent; import mx.rpc.events.ResultEvent; protected function resultHandler(event:ResultEvent):void { Alert.show(event.result.toString(), "提示"); //Alert.show("连接成功!"); } protected function faultHandler(event:FaultEvent):void { Alert.show(event.fault.toString(), "提示"); //Alert.show("连接失败!"); } protected function reseiveHandler(event:MouseEvent):void { Alert.show("接受用户消息"); } protected function login_Handler(event:MouseEvent):void { var username:String = txt_username.text; var password:String = txt_password.text; remoteObject.login(username , password); } ]]--> </fx:Script> <fx:Declarations> <!-- Place non-visual elements (e.g., services, value objects) here --> <mx:RemoteObject id="remoteObject" destination="helloworld" result="resultHandler(event)" fault="faultHandler(event)"/> </fx:Declarations> <mx:Form width="100%" height="251"> <mx:FormItem> <mx:label>用户名:</mx:label> <s:TextInput id="txt_username"/> </mx:FormItem> <mx:FormItem> <mx:label>密码:</mx:label> <s:TextInput id="txt_password"/> </mx:FormItem> <mx:FormItem height="22"> <mx:Button id="btn_login" label="登录" click="login_Handler(event)"/> </mx:FormItem> </mx:Form> </s:Application>
还要修改java项目的WebContent/WEB-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="helloworld"> <properties> <source>com.blazeDS.test.HelloWorld</source> </properties> </destination> </service>
最后启动tomcat ,输入网址就能访问了。