加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

flex与java集成

发布时间:2020-12-15 04:39:19 所属栏目:百科 来源:网络整理
导读:HelloWord.java @Service("com.wecups.test.HelloWorld") @RemotingDestination public class HelloWorld { ? ?public String sayHello(String name) ?{ ??return "Hello! "+name; ?} } ApplicationContext.mxml(这个是parsley框架需要的,相当于flex的spri

HelloWord.java

@Service("com.wecups.test.HelloWorld")
@RemotingDestination
public class HelloWorld {

?
?public String sayHello(String name)
?{
??return "Hello! "+name;
?}
}

ApplicationContext.mxml(这个是parsley框架需要的,相当于flex的spring,在这个配置文件中声明了就可以直接使用[Inject]依赖注入)

<?xml version="1.0" encoding="utf-8"?>
<Object xmlns="*"
??xmlns:fx="http://ns.adobe.com/mxml/2009"
??xmlns:mx="library://ns.adobe.com/flex/mx"
??xmlns:service="com.wecups.test.service.*" >
?<fx:Declarations>
??<service:HelloService/>
?</fx:Declarations>
</Object>

BaseRemoteService.as

package com.wecups.test.service
{
?import flash.events.EventDispatcher;
?import flash.events.IEventDispatcher;
?
?import mx.core.FlexGlobals;
?import mx.rpc.remoting.RemoteObject;
?
?public class BaseRemoteService extends EventDispatcher
?{
??private var _ro:RemoteObject;
??public var destination:String;
??
??public function BaseRemoteService()
??{
???
??}
??
??public function get ro():RemoteObject
??{
???_ro = new RemoteObject();
???var contextRoot:String = FlexGlobals.topLevelApplication.contextPath;(这个在flex主应用程序中定义,与工程属性的flex服务器中的上下文环境对应)

//与services-config.xml中的endpoint一致
???_ro.endpoint =FlexGlobals.topLevelApplication.contextPath+"/spring/messagebroker/amf";
//????"http://localhost:8080"+contextRoot+"/spring/messagebroker/amf";
????
???_ro.destination = destination;
???return _ro;
??}
?}
}

?

HelloService.as

package com.wecups.test.service
{
?import mx.rpc.AsyncToken;

?public class HelloService extends BaseRemoteService
?{
??public function HelloService()
??{
???super();
???destination = "com.wecups.test.HelloWorld";(这个是java端的服务类的service注解上边的路径)
??}
??
??public function sayHello(name:String):AsyncToken
??{
???return ro.sayHello(name);
??}
?}
}

?

frameTest.mxml(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:parsley="http://www.spicefactory.org/parsley"
????? xmlns:mx="library://ns.adobe.com/flex/mx"
????? minWidth="955" minHeight="600"
????? creationComplete="creationCompleteHandler(event)"
????? >
?
?<fx:Script>
??<![CDATA[
???import com.wecups.test.service.HelloService;
???
???import mx.controls.Alert;
???import mx.core.FlexGlobals;
???import mx.events.FlexEvent;
???import mx.messaging.messages.RemotingMessage;
???import mx.rpc.AsyncResponder;
???import mx.rpc.AsyncToken;
???import mx.rpc.events.FaultEvent;
???import mx.rpc.events.ResultEvent;
???
???public var contextPath:String = "/frameTest";
???
???[Inject]
???public var helloService:HelloService;
???protected function btn_clickHandler(event:MouseEvent):void
???{
????helloService.sayHello("tracy").addResponder(new
?????AsyncResponder(getData,FlexGlobals.topLevelApplication.defaultError));
???}
???
???private function getData(event:ResultEvent,token:AsyncToken):void
???{
????var result:String = String(event.result);
????Alert.show(result);
???}
???
???public function defaultError(error:FaultEvent,token:Object=null):void
???{
????var errorLog:String = "Code:"+error.fault.faultCode+"r";
????errorLog = errorLog +"Fault:"+error.fault.faultString+"r";
????errorLog = errorLog +"Java Class:"+error.token.message.destination+"r";
????errorLog = errorLog +"Function:"+RemotingMessage(error.token.message).operation+"";
????Alert.show(errorLog);
???}
???
???protected function doLoadClick(event:MouseEvent):void
???{
????showText.text="load Module";
????loader.loadModule();
???}
???
???protected function doUnloadClick(event:MouseEvent):void
???{
????showText.text="unload Module";
????loader.unloadModule();
???}
???

???protected function creationCompleteHandler(event:FlexEvent):void
???{
//????doLoadClick(null);
???}

??]]>
?</fx:Script>
?
?<fx:Declarations>

<!--声明parsley框架-->
??<parsley:ContextBuilder config="com.wecups.test.context.ApplicationContext"/>
??<parsley:Configure/>
?</fx:Declarations>
?<s:VGroup>
??<s:HGroup>
???<s:Button id="btn" label="sayHello" click="btn_clickHandler(event)" />
???
???<s:Button label="load"? click="doLoadClick(event)"/>
???<s:Button label="unload" click="doUnloadClick(event)"/>
??</s:HGroup>
??<s:BorderContainer width="500" height="600">
???<s:Label id="showText"/>
???<mx:ModuleLoader id="loader"? url="./com/wecups/test/ModuleDemo.swf"/>
??</s:BorderContainer>
?</s:VGroup>
</s:Application>

?

flex库有四个配置文件

1,修改services-config.xml的endpoint

<?xml version="1.0" encoding="UTF-8"?>
<services-config>

??? <services>
??????? <service-include file-path="remoting-config.xml" />
??????? <service-include file-path="proxy-config.xml" />
??????? <service-include file-path="messaging-config.xml" />?
??????? <default-channels>
???<channel ref="my-amf"/>
??</default-channels>?????
??? </services>

??? <security>
??????? <login-command class="flex.messaging.security.TomcatLoginCommand" server="Tomcat"/>
??????? <!-- Uncomment the correct app server
??????? <login-command class="flex.messaging.security.TomcatLoginCommand" server="JBoss">
??<login-command class="flex.messaging.security.JRunLoginCommand" server="JRun"/>???????
??????? <login-command class="flex.messaging.security.WeblogicLoginCommand" server="Weblogic"/>
??????? <login-command class="flex.messaging.security.WebSphereLoginCommand" server="WebSphere"/>
??????? -->

??????? <!--
??????? <security-constraint id="basic-read-access">
??????????? <auth-method>Basic</auth-method>
??????????? <roles>
??????????????? <role>guests</role>
??????????????? <role>accountants</role>
??????????????? <role>employees</role>
??????????????? <role>managers</role>
??????????? </roles>
??????? </security-constraint>
???????? -->
??? </security>

??? <channels>

??????? <channel-definition id="my-amf" class="mx.messaging.channels.AMFChannel">
??????????? <endpoint url="http://{server.name}:{server.port}/{context.root}/spring/messagebroker/amf" class="flex.messaging.endpoints.AMFEndpoint"/>
??????? </channel-definition>

??????? <channel-definition id="my-secure-amf" class="mx.messaging.channels.SecureAMFChannel">
??????????? <endpoint url="https://{server.name}:{server.port}/{context.root}/spring/messagebroker/amfsecure" class="flex.messaging.endpoints.SecureAMFEndpoint"/>
??????????? <properties>
??????????????? <add-no-cache-headers>false</add-no-cache-headers>
??????????? </properties>
??????? </channel-definition>

??????? <channel-definition id="my-polling-amf" class="mx.messaging.channels.AMFChannel">
??????????? <endpoint url="http://{server.name}:{server.port}/{context.root}/spring/messagebroker/amfpolling" class="flex.messaging.endpoints.AMFEndpoint"/>
??????????? <properties>
??????????????? <polling-enabled>true</polling-enabled>
??????????????? <polling-interval-seconds>4</polling-interval-seconds>
??????????? </properties>
??????? </channel-definition>
???????
??????? <channel-definition id="my-streaming-amf"
???class="mx.messaging.channels.StreamingAMFChannel">
???<endpoint
????url="http://{server.name}:{server.port}/{context.root}/spring/messagebroker/streamingamf"
????class="flex.messaging.endpoints.StreamingAMFEndpoint" />
</channel-definition>
??????? <!--
??????? <channel-definition id="my-http" class="mx.messaging.channels.HTTPChannel">
??????????? <endpoint url="http://{server.name}:{server.port}/{context.root}/messagebroker/http" class="flex.messaging.endpoints.HTTPEndpoint"/>
??????? </channel-definition>

??????? <channel-definition id="my-secure-http" class="mx.messaging.channels.SecureHTTPChannel">
??????????? <endpoint url="https://{server.name}:{server.port}/{context.root}/messagebroker/httpsecure" class="flex.messaging.endpoints.SecureHTTPEndpoint"/>
??????????? <properties>
??????????????? <add-no-cache-headers>false</add-no-cache-headers>
??????????? </properties>
??????? </channel-definition>
??????? -->
??? </channels>

??? <logging>
??????? <target class="flex.messaging.log.ConsoleTarget" level="Error">
??????????? <properties>
??????????????? <prefix>[BlazeDS] </prefix>
??????????????? <includeDate>false</includeDate>
??????????????? <includeTime>false</includeTime>
??????????????? <includeLevel>false</includeLevel>
??????????????? <includeCategory>false</includeCategory>
??????????? </properties>
??????????? <filters>
??????????????? <pattern>Endpoint.*</pattern>
??????????????? <pattern>Service.*</pattern>
??????????????? <pattern>Configuration</pattern>
??????????? </filters>
??????? </target>
??? </logging>

??? <system>
??????? <redeploy>
??????????? <enabled>false</enabled>
??????????? <!--
??????????? <watch-interval>20</watch-interval>
??????????? <watch-file>{context.root}/WEB-INF/flex/services-config.xml</watch-file>
??????????? <watch-file>{context.root}/WEB-INF/flex/proxy-config.xml</watch-file>
??????????? <watch-file>{context.root}/WEB-INF/flex/remoting-config.xml</watch-file>
??????????? <watch-file>{context.root}/WEB-INF/flex/messaging-config.xml</watch-file>
??????????? <watch-file>{context.root}/WEB-INF/flex/data-management-config.xml</watch-file>
??????????? <touch-file>{context.root}/WEB-INF/web.xml</touch-file>
???????????? -->
??????? </redeploy>
??? </system>

</services-config>

?2,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>

</service>

3,proxy-config.xml

<?xml version="1.0" encoding="UTF-8"?>
<service id="proxy-service"
??? class="flex.messaging.services.HTTPProxyService">

??? <properties>
??????? <connection-manager>
??????????? <max-total-connections>100</max-total-connections>
??????????? <default-max-connections-per-host>2</default-max-connections-per-host>
??????? </connection-manager>
??????? <allow-lax-ssl>true</allow-lax-ssl>
??? </properties>

??? <adapters>
??????? <adapter-definition id="http-proxy" class="flex.messaging.services.http.HTTPProxyAdapter" default="true"/>
??????? <adapter-definition id="soap-proxy" class="flex.messaging.services.http.SOAPProxyAdapter"/>
??? </adapters>

??? <default-channels>
??????? <channel ref="my-amf"/>
??? </default-channels>

??? <destination id="DefaultHTTP">
??? </destination>

</service>

4,messaging-config.xml

<?xml version="1.0" encoding="UTF-8"?>
<service id="message-service"
??? class="flex.messaging.services.MessageService">

??? <adapters>
??????? <adapter-definition id="actionscript" class="flex.messaging.services.messaging.adapters.ActionScriptAdapter" default="true" />
??????? <!-- <adapter-definition id="jms" class="flex.messaging.services.messaging.adapters.JMSAdapter"/> -->
??? </adapters>

??? <default-channels>
??????? <channel ref="my-amf"/>
??????? <channel ref="my-polling-amf"/>
??? </default-channels>

</service>

?

配置applicationContext_flex.xml

?

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
?xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
?xmlns:context="http://www.springframework.org/schema/context"
?xmlns:flex="http://www.springframework.org/schema/flex" xmlns:jee="http://www.springframework.org/schema/jee"
?xmlns:tx="http://www.springframework.org/schema/tx" xmlns:tool="http://www.springframework.org/schema/tool"
?xsi:schemaLocation="
???http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans-2.5.xsd
???http://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context-2.5.xsd
???http://www.springframework.org/schema/jeehttp://www.springframework.org/schema/jee/spring-jee-2.5.xsd
???http://www.springframework.org/schema/txhttp://www.springframework.org/schema/tx/spring-tx-2.5.xsd
???http://www.springframework.org/schema/flexhttp://www.springframework.org/schema/flex/spring-flex-1.0.xsd
???http://www.springframework.org/schema/toolhttp://www.springframework.org/schema/tool/spring-tool-2.5.xsd">
<!-- Bootstraps and exposes the BlazeDS MessageBroker -->
?<context:annotation-config />
?<context:component-scan base-package="com.wecups" />
?<bean id="_messageBroker" class="org.springframework.flex.core.MessageBrokerFactoryBean">
?</bean>
?<bean
??class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
?</bean>
?<!-- Dispatches requests mapped to a MessageBroker -->
?<bean class="org.springframework.flex.servlet.MessageBrokerHandlerAdapter">
?</bean>
?<!-- Maps request paths at /* to the BlazeDS MessageBroker -->
?<bean class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
??<property name="mappings">
???<value>
????/messagebroker/*=_messageBroker
???</value>
??</property>
?</bean>

?<bean
??class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping" />

?<bean class="org.springframework.flex.config.RemotingAnnotationPostProcessor" />
?<bean
??class="org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor" />
?<bean id="multipartResolver"
??class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
??<property name="maxUploadSize" value="10000000" />
?</bean>
</beans>??

配置项目的web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
?xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
?xsi:schemaLocation="http://java.sun.com/xml/ns/javaeehttp://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
?id="WebApp_ID" version="2.5">

?<!-- <display-name>BlazeDS</display-name> -->
?<!-- <description>BlazeDS Application</description> -->

?<context-param>
??<param-name>contextConfigLocation</param-name>
??<param-value>/WEB-INF/spring/applicationContext.xml</param-value>
?</context-param>

?<!-- Http Flex Session attribute and binding listener support -->
?<listener>
??<listener-class>flex.messaging.HttpFlexSession</listener-class>
?</listener>
?<listener>
??<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
?</listener>
?<listener>
??<listener-class>org.springframework.web.util.IntrospectorCleanupListener</listener-class>
?</listener>

?<!-- MessageBroker Servlet -->
?<!-- <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> -->

?<servlet>
??<servlet-name>Spring MVC Dispatcher Servlet</servlet-name>
??<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
??<init-param>
???<param-name>contextConfigLocation</param-name>
???<param-value>WEB-INF/spring/applicationContext_flex.xml</param-value>
??</init-param>
??<load-on-startup>1</load-on-startup>
?</servlet>
?<servlet-mapping>
??<servlet-name>Spring MVC Dispatcher Servlet</servlet-name>
??<url-pattern>/spring/*</url-pattern>
?</servlet-mapping>
?<servlet>
??<servlet-name>RDSDispatchServlet</servlet-name>
??<servlet-class>flex.rds.server.servlet.FrontEndServlet</servlet-class>
??<init-param>
???<param-name>useAppserverSecurity</param-name>
???<param-value>false</param-value>
??</init-param>
??<init-param>
???<param-name>messageBrokerId</param-name>
???<param-value>_messageBroker</param-value>
??</init-param>
??<load-on-startup>10</load-on-startup>
?</servlet>

?<servlet-mapping>
??<servlet-name>RDSDispatchServlet</servlet-name>
??<url-pattern>/CFIDE/main/ide.cfm</url-pattern>
?</servlet-mapping>
?<welcome-file-list>
??<welcome-file>frameTest.html</welcome-file>
??<welcome-file>index.htm</welcome-file>
?</welcome-file-list>
?
?<!-- struts配置 -->
?<servlet>
??<servlet-name>actionServlet</servlet-name>
??<servlet-class>org.apache.struts.action.ActionServlet</servlet-class>
??<init-param>
???<param-name>strutsConf</param-name>
???<param-value>/WEB-INF/struts-config.xml</param-value>
??</init-param>
??<load-on-startup>1</load-on-startup>
?</servlet>
?<servlet-mapping>
??<servlet-name>actionServlet</servlet-name>
??<url-pattern>*.do</url-pattern>
?</servlet-mapping>

</web-app>

别忘了假如需要的jar包,就可以跑起来了

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读