手把手教你使用flex eclipse整合spring
最先下载FlashBuilder_4_7_LS10_win64.exe试了几个eclipse安装插件都没成功,包括myeclipse8.5、springsts2.9.2、eclipse3.5、j2eeeclipse版本4.2.0,后来搞了一个FlashBuilder_4_LS10.exe安装完找不到插件安装文件原来这个是单独版,必须插件版才行,最后下载FlashBuilder_4_Plugin_LS10.exe终于配置成功了,myeclipse8.5不行,springsts可以了。
springsts部署应用跟myeclipse不一样,比较类似eclipse。
用sts整合flex和java有几个步骤:
1:新建动态web工程flexweb,创建web.xml
2:blazeds-turnkey-4.0.0.14931.zip解压,复制blazed两个文件夹flex和lib到WEB-INF下,里面是blaze的jar包和flex配置文件,然后修改web.xml加入blaze支持
<listener> <listener-class>flex.messaging.HttpFlexSession</listener-class> </listener> <!--MessageBrokerServlet--> <servlet> <servlet-name>MessageBrokerServlet</servlet-name> <servlet-class>flex.messaging.MessageBrokerServlet</servlet-class> <init-param> <param-name>services.configuration.file</param-name> <param-value>/WEB-INF/flex/services-config.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>MessageBrokerServlet</servlet-name> <url-pattern>/messagebroker/*</url-pattern> </servlet-mapping>
3:项目右键,添加/更改项目类型>添加flex类型项目,第一步,应用程序类型选择J2EE,下方选择BlazeDS,第二部根文件夹填入项目在workspase的路径加一个WebContent,如E:\workspaces\sts\flexweb\WebContent,根URL填http://localhost:8080/flexweb,上下文根目录/flexweb,输出文件夹使用默认E:\workspaces\sts\flexweb\WebContent\flexweb-debug,点击finish
4:转换完成后,目录有些变化,右键项目>properties>flex构建路径,主源文件夹改为flex_src,然后把自动生成的src目录下的flexweb.mxml移动到flex_src下,环境搭建就算完成了
HelloWorld
flexweb.mxml:
<?xmlversion="1.0"encoding="utf-8"?>
<s:Applicationxmlns: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[
importmx.controls.Alert;
importmx.rpc.events.ResultEvent;
protectedfunctionmyFlex_resultHandler(event:ResultEvent):void{
varname:String=event.resultasString;
Alert.show(name);
}
protectedfunctionbutton1_clickHandler(event:MouseEvent):void
{
myFlex.sayHello(txtName.text);
}
]]>
</fx:Script>
<fx:Declarations>
<!--将非可视元素(例如服务、值对象)放在此处-->
<s:RemoteObjectid="myFlex"destination="mytest"result="myFlex_resultHandler(event)"/>
</fx:Declarations>
<s:Buttonx="209"y="135"label="按钮"click="button1_clickHandler(event)"/>
<s:TextInputx="166"y="81"id="txtName"/>
<s:Labelx="10"y="81"text="请输入内容:"fontSize="15"fontWeight="bold"fontFamily="中易黑体"/>
</s:Application>
其中
<s:RemoteObjectid="myFlex"destination="mytest"result="myFlex_resultHandler(event)"/>
指定了一个调用Java的类Hello,mytest对应到remoting-config.xml
在WEB-INFO/flex目录下remoting-config.xml加入
<destinationid="mytest"> <properties> <source>com.hongbo.Hello</source> </properties> </destination>
result对应的是java方法调用的回调函数
建一个普通java类
packagecom.hongbo;
publicclassHello{
publicStringsayHello(Stringname){
System.out.println("------------------------------------");
return"HelloFirstDemo"+name;
}
}
这样就OK了
访问路径是http://localhost:8080/flexweb/flexweb-debug/flexweb.html
第一次会报404,problems提示无法创建html包装器,右键点击重新创建模板
添加Spring支持
1:web.xml加入
<context-param> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/classes/applicationContext.xml</param-value> </context-param> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener>
2:src下创建applicationContext.xml
<?xmlversion="1.0"encoding="UTF-8"?> <beansxmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans-2.0.xsd http://www.springframework.org/schema/aophttp://www.springframework.org/schema/aop/spring-aop-2.0.xsd http://www.springframework.org/schema/txhttp://www.springframework.org/schema/tx/spring-tx-2.0.xsd"> <beanid="hello"class="com.hongbo.Hello"> <propertyname="testSpring"> <refbean="testSpring"/> </property> </bean> <beanid="testSpring"class="com.hongbo.test.impl.TestSpringImpl"/> </beans>
3:WEB-INF/flex/service-config.xml加入
<factories> <factoryid="spring"class="com.hongbo.SpringFactory"/> </factories>
添加java类
packagecom.hongbo;
importorg.springframework.beans.BeansException;
importorg.springframework.beans.factory.NoSuchBeanDefinitionException;
importorg.springframework.context.ApplicationContext;
importorg.springframework.web.context.support.WebApplicationContextUtils;
importflex.messaging.FactoryInstance;
importflex.messaging.FlexFactory;
importflex.messaging.config.ConfigMap;
importflex.messaging.services.ServiceException;
publicclassSpringFactoryimplementsFlexFactory{
privatestaticfinalStringSOURCE="source";
publicvoidinitialize(Stringid,ConfigMapconfigMap){
}
publicFactoryInstancecreateFactoryInstance(Stringid,ConfigMapproperties){
SpringFactoryInstanceinstance=newSpringFactoryInstance(this,id,
properties);
instance.setSource(properties.getPropertyAsString(SOURCE,instance
.getId()));
returninstance;
}//endmethodcreateFactoryInstance()
publicObjectlookup(FactoryInstanceinst){
SpringFactoryInstancefactoryInstance=(SpringFactoryInstance)inst;
returnfactoryInstance.lookup();
}
staticclassSpringFactoryInstanceextendsFactoryInstance{
SpringFactoryInstance(SpringFactoryfactory,Stringid,
ConfigMapproperties){
super(factory,id,properties);
}
publicStringtoString(){
return"SpringFactoryinstanceforid="+getId()+"source="
+getSource()+"scope="+getScope();
}
publicObjectlookup(){
ApplicationContextappContext=WebApplicationContextUtils.getWebApplicationContext(flex.messaging.FlexContext.getServletConfig().getServletContext());
StringbeanName=getSource();
try{
returnappContext.getBean(beanName);
}catch(NoSuchBeanDefinitionExceptionnexc){
ServiceExceptione=newServiceException();
Stringmsg="Springservicenamed'"+beanName
+"'doesnotexist.";
e.setMessage(msg);
e.setRootCause(nexc);
e.setDetails(msg);
e.setCode("Server.Processing");
throwe;
}catch(BeansExceptionbexc){
ServiceExceptione=newServiceException();
Stringmsg="UnabletocreateSpringservicenamed'"
+beanName+"'";
e.setMessage(msg);
e.setRootCause(bexc);
e.setDetails(msg);
e.setCode("Server.Processing");
throwe;
}
}
}
}
4:修改remoting-config.xml
<destinationid="mytest"> <properties> <factory>spring</factory> <source>hello</source> </properties> </destination>
5:修改相应的Java类
packagecom.hongbo;
importcom.hongbo.test.TestSpring;
publicclassHello{
privateTestSpringtestSpring;
publicvoidsetTestSpring(TestSpringtestSpring){
this.testSpring=testSpring;
}
publicStringsayHello(Stringname){
returntestSpring.testSpring(name);
}
}
packagecom.hongbo.test;
publicinterfaceTestSpring{
StringtestSpring(Stringname);
}
packagecom.hongbo.test.impl;
importcom.hongbo.test.TestSpring;
publicclassTestSpringImplimplementsTestSpring{
publicStringtestSpring(Stringname){
System.out.println("testspring-------------------------------------"+name);
return"testspring"+name;
}
}
最后,flex打印语句trace不会打印到控制台,要先卸载flashplayer再安装一个debuger版的flashplayer,下载flashplayer_uninstall.zip,卸载,下载flashplayer10r12_36_winax_debug.exe,安装,卸载安装后好像谷歌浏览器没影响,然后eclipse修改默认浏览器为IE,window>preferences>General>Webbrowser,选择InternetExplorer,最后还有,启动tomcat后,必须在mxml上面右键debug运行,打开的IE才会打印trace,直接访问网址是不行的。
如有遗漏请指出