BlazeDS使用例子

来源:互联网 发布:淘宝入驻知名品牌 编辑:程序博客网 时间:2024/05/14 19:34

建立maven web项目,结构如下


webapp展开如下


pom.xml中引用spring-flex-core:1.5.2.RELEASE即可。

下载blazeds-bin-4.0.0.14931.zip,在war包blazeds\WEB-INF\flex下面有四个xml文件,复制到所建项目WEB-INF下。

如果与spring集成,可以自建spring-flex-config.xml如下,为方便管理,可以与刚才的四个xml放一起。

<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans"xmlns:flex="http://www.springframework.org/schema/flex" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans-3.0.xsdhttp://www.springframework.org/schema/flexhttp://www.springframework.org/schema/flex/spring-flex-1.0.xsd">  <flex:message-broker><flex:remoting-service default-channels="my-amf"/></flex:message-broker></beans>
applicationContext.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:context="http://www.springframework.org/schema/context"xmlns:aop="http://www.springframework.org/schema/aop"xmlns:tx="http://www.springframework.org/schema/tx"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd">    <description>Spring公共配置文件 </description><context:component-scan base-package="com.hdsx.dms" /><context:annotation-config/></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/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" version="3.0"> <display-name>dmsystem</display-name><context-param><param-name>contextConfigLocation</param-name><param-value>classpath:applicationContext.xml</param-value></context-param><listener><listener-class>org.springframework.web.context.ContextLoaderListener</listener-class></listener><listener><listener-class>org.springframework.web.util.IntrospectorCleanupListener</listener-class></listener>  <!-- Http Flex Session attribute and binding listener support -->    <listener>        <listener-class>flex.messaging.HttpFlexSession</listener-class>    </listener>        <!-- MessageBroker Servlet -->    <servlet><servlet-name>messagebroker</servlet-name><servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class><init-param><param-name>contextConfigLocation</param-name><param-value>/WEB-INF/flex/spring-flex-config.xml</param-value></init-param><load-on-startup>1</load-on-startup></servlet><servlet-mapping><servlet-name>messagebroker</servlet-name><url-pattern>/messagebroker/*</url-pattern></servlet-mapping>  <welcome-file-list>    <welcome-file>index.jsp</welcome-file></welcome-file-list></web-app>
下面来看java后台。代码如下

public class User {private String id;private String userName;private String password;
package com.hdsx.dms.user;import org.springframework.flex.remoting.RemotingDestination;@RemotingDestinationpublic interface UserService {String save(User o);}
package com.hdsx.dms.user;import org.springframework.stereotype.Service;@Servicepublic class UserServiceImpl implements UserService{@Overridepublic String save(User o) {System.out.println(o.toString());return o.toString();}}
Flex项目架构如下

Flex编译器附加参数如下图

Flex服务器选择Java,配置如下

User类与java后台一致

[RemoteClass(alias="com.hdsx.dms.user.User")]public class User{private var _id:String;private var _userName:String;private var _password:String;

index.mxml代码如下

<?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:Declarations><!-- 将非可视元素(例如服务、值对象)放在此处 --></fx:Declarations><fx:Script><![CDATA[import com.hdsx.dms.user.User;import mx.rpc.AbstractOperation;import mx.rpc.events.ResultEvent;import mx.rpc.remoting.RemoteObject;protected function saveUser(event:MouseEvent):void{var user:User = new User();user.id = "1";user.userName = "wusq";user.password = "000000";var remoteObject:RemoteObject = new RemoteObject();remoteObject.endpoint = "http://127.0.0.1:8080/dmsystem/messagebroker/amf";remoteObject.destination = "userServiceImpl";var method:AbstractOperation= remoteObject.getOperation("save");method.addEventListener(ResultEvent.RESULT,resultHander);method.send(user);}protected function resultHander(event:ResultEvent):void{trace(event.result);}]]></fx:Script><s:Button x="27" y="23" label="saveUser" click="saveUser(event)"/></s:Application>


over。




原创粉丝点击