Flex4.0+pureMVC+JAVA

-------------------PureMVCTest.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"
      creationComplete="facade.startup(this)" backgroundColor="#FFFEFE"><!--启动Facade-->
 <fx:Script>
  <![CDATA[
   import mvc.*;
   import mvc.vo.LoginVO;
   
   
   /**
    import com.me.myapp.ApplicationFacade;
    import mvc.controller.PureCommand;
    import mx.events.FlexEvent;
    public static var main:pureMVCdemo;
    protected function application1_creationCompleteHandler(event:FlexEvent):void
    {
    Facade.getInstance().registerCommand(PureNotify.START,PureCommand);
    Facade.getInstance().sendNotification(PureNotify.START);
    main = this;
    }
    **/
   
   /**
    * 获取ApplicationFacade实例,
    * 并在头部调用该实例的startup方法进行一系列实例化,
    * 一般只在顶层的Application初始化Facade
    **/
   private var facade:ApplicationFacade=ApplicationFacade.getInstance();
   
   
   [Bindable]public var loginVO:LoginVO=new LoginVO();
   
   //测试1
   protected function button1_clickHandler(event:MouseEvent):void
   {
    //Facade.getInstance().sendNotification(PureNotify.CMD_OPEN);
    this.facade.sendNotification(PureNotify.CMD_OPEN);//进行一个Notification(通知)的发送,将在控制器的execute()方法中接收到此通知
   }
   
   //测试2
   protected function button2_clickHandler(event:MouseEvent):void
   {
    this.facade.sendNotification(PureNotify.SIMPLECOMMAND_LOGIN_VERIFY,loginVO);
    //this.facade.sendNotification(PureNotify.LOGIN,loginVO);
   }
  ]]>
 </fx:Script>
 
 <fx:Binding source="username.text" destination="loginVO.username"></fx:Binding>
 <fx:Binding source="password.text" destination="loginVO.password"></fx:Binding>
 
 <fx:Declarations>
  <!-- 将非可视元素(例如服务、值对象)放在此处 -->
 </fx:Declarations>
 <s:BorderContainer y="88" width="417" height="180" backgroundColor="#E7E8EC" backgroundAlpha="0.59" horizontalCenter="-57">
  <s:TextArea x="10" y="44" id="content" width="389" fontFamily="宋体" fontWeight="bold" color="#E80A0A" text="sssss" height="121"/>
  <s:Button x="10" y="10" label="请求数据" click="button1_clickHandler(event)"/>
 </s:BorderContainer>
 
 
 <s:Panel id="panel" y="296" width="417" height="177" horizontalCenter="-57">
  <s:Label x="52" y="21" text="Username"/>
  <s:TextInput x="143" y="11" text="{loginVO.username}" id="username" width="246"/>
  <s:Label x="52" y="70" text="Password"/>
  <s:TextInput x="143" y="60" text="{loginVO.password}" id="password" displayAsPassword="true" width="245"/>
  <s:Button id="bu" label="Login" click="button2_clickHandler(event)" x="144" y="100"/>
 </s:Panel>
 
</s:Application>

 

 ---------------------PureCommand.as

package mvc.controller
{
 import mvc.PureNotify;
 import mvc.model.PureProxy;
 import mvc.po.UserInfo;
 import mvc.vo.LoginVO;
 
 import mx.controls.*;
 import mx.rpc.AbstractOperation;
 import mx.rpc.AsyncToken;
 import mx.rpc.events.FaultEvent;
 import mx.rpc.events.ResultEvent;
 
 import org.puremvc.as3.interfaces.INotification;
 import org.puremvc.as3.patterns.command.SimpleCommand;
 
 /**
  * Controller保存所有Command的映射。Command可以获取Proxy对象并与之交互,通过发送Notification来执行其他的Command
  * 充当控制器
  **/
 public class PureCommand extends SimpleCommand
 {
  public function PureCommand()
  {
   super();
  }
  
  private var loginVO:LoginVO;
  private var operation:AbstractOperation;
  
  /**
   * 获取模型实例
   **/
  private function get proxy():PureProxy
  {
   return this.facade.retrieveProxy(PureProxy.NAME) as PureProxy;
  }
  
  /**
   * 所有的通知将在此接收
   * 接收Facade的Notification(通知)以及客户端的Notification(通知)
   * 并调用模型中的某个业务方法
   **/
  override public function execute(notification:INotification):void
  {
   switch(notification.getName())
   {
    case PureNotify.CMD_OPEN:
     proxy.CMD_OPENFunc();//获取Model实例并调用Model中的业务方法
     break;
    case PureNotify.SIMPLECOMMAND_LOGIN_VERIFY:
     this.loginVO=notification.getBody() as LoginVO;
     operation=proxy.userInfoVerify(notification.getBody());//获取操作信息
     operation.addEventListener(ResultEvent.RESULT, onResultHandler);//onResultHandler是一个内嵌函数
     operation.addEventListener(FaultEvent.FAULT, onFaultHandler);
     var token:AsyncToken = operation.send();
     token.invokeTime = new Date();
     break;
   }
  }
  
  
  
  /**
   * 声明调用成功的监听函数
   * 显示返回结果   
   **/
  private function onResultHandler(event:ResultEvent):void{
   operation.removeEventListener(ResultEvent.RESULT, onResultHandler);//一定要删除以前的监听器(防止重复监听事件),以防止弹出重复对话框
   var userInfo:UserInfo=event.result as UserInfo;
   var loginVO:LoginVO=this.loginVO;
   if(loginVO.username==userInfo.username&&loginVO.password==userInfo.password){
    Alert.show("登录成功");
   }else{
    Alert.show("登录失败");
   }
  }
  
  /**
   * 声明调用失败的监听函数
   * 显示错误信息
   **/
  private function onFaultHandler(event:FaultEvent):void{
   operation.removeEventListener(FaultEvent.FAULT, onFaultHandler);//防止弹出重复对话框
   Alert.show(event.message.toString(),"调用失败");
  }
 }
}

 

 -------------------------StartCommand.as

package mvc.controller
{
 import mvc.PureNotify;
 
 import org.puremvc.as3.interfaces.INotification;
 import org.puremvc.as3.patterns.command.SimpleCommand;
 
 //在子类中进行一系列相应的初始化工作
 public class StartCommand extends SimpleCommand
 {
  public function StartCommand()
  {
   super();
  }
  
  override public function execute(notification:INotification):void{
   //notification.getBody();
   switch(notification.getName()){
    case PureNotify.START:
     
     break;
   }
   
  }
 }
}

 

--------------------PureProxy.as

package mvc.model
{
 
 import mvc.ApplicationFacade;
 import mvc.PureNotify;
 
 import mx.controls.*;
 import mx.rpc.AbstractOperation;
 import mx.rpc.remoting.RemoteObject;
 
 import org.puremvc.as3.interfaces.IProxy;
 import org.puremvc.as3.patterns.proxy.Proxy;
 
 /**
  * Model保存对Proxy对象的引用,Proxy负责操作数据模型,与远程服务通信存取数据
  * 为了降低耦合度Proxy一般只发送Notification而不接受Notification
  * 充当模型
  **/
 public class PureProxy extends Proxy implements IProxy
 {
  
  public static const NAME:String = "mvc.model.PureProxy";
  
  //使用RemoteObject组件最终访问的是远程Java对象公 开的公共方法。调用远程Java对象方法的最简单方式即直接调用方法名
  private var remoteObject:RemoteObject;
  
  public function PureProxy(proxyName:String=null, data:Object=null)
  {
   super(proxyName, data);
   remoteObject=new RemoteObject();
   remoteObject.source="com.my.test.daoi.UserImpl";
   remoteObject.destination="userImpl";
  }
  
  /**
   * 接收来自控制器的调用,并将结果返回给View
   *CMD_OPEN 后台请求数据
   */
  public function CMD_OPENFunc():void
  {
   var obj:Object = {text:"pureMVCdemo测试"};
   this.facade.sendNotification(PureNotify.VIEW_OPEN,obj);//“PureNotify.VIEW_OPEN”是View(Mediator)所关心的Notification,因此它下一步会请求到View中
  }
  
  /**
   *VIEW_OPEN 前台显示数据
   */
  public function VIEW_OPENFunc(obj:Object):void
  {
   Alert.show("content="+ApplicationFacade.main.content.text.toString());
   ApplicationFacade.main.content.text=obj.text;
  }
  
  /**
   *获取用户信息
   **/
  public function userInfoVerify(obj:Object):AbstractOperation{
   var operation:AbstractOperation = remoteObject.getOperation("getUserInfo");
   return operation;

  }
 }
}

 

 --------------UserInfo.as

package mvc.po
{
 //把这个AS3 VO映射到Remote Class
 [RemoteClass(alias="com.my.test.pojo.User")]
 public class UserInfo
 {
  public function UserInfo()
  {
  }
  
  public  var username:String;
  public  var password:String;
  
  public function  get getUsername():String {
   return this.username;
  }
  
  public function  set setUsername(username:String):void {
   this.username = username;
  }
  
  public function  get getPasswords():String {
   return this.password;
  }
  
  public function  set setPasswords(password:String):void {
   this.password = password;
  }
  
 }
}

 

--------------------PureMediator.as

package mvc.view
{
 import mvc.PureNotify;
 import mvc.model.PureProxy;
 
 import mx.controls.Alert;
 
 import org.puremvc.as3.interfaces.IMediator;
 import org.puremvc.as3.interfaces.INotification;
 import org.puremvc.as3.patterns.mediator.Mediator;
 
 /**
  * View保存对Mediator对象的引用。由Mediator对象来操作具体的视图组件(View Component,例如Flex的DataGrid组件),包括:添加事件监听器,发送或接收Notification ,直接改变视图组件的状态
  * 充当视图(监听那些和视图有关的Notification)
  **/
 public class PureMediator extends Mediator implements IMediator
 {
  //每个Mediator都应该有一个名字(将用于注册时使用),名字的规范为包名+类名
  public static const Name:String = "mvc.view.PureMediator";
  
  public function PureMediator(mediatorName:String=null, viewComponent:Object=null)
  {
   super(mediatorName, viewComponent);
   
  }
  
  //获取模型实例
  private function get proxy():PureProxy
  {
   return this.facade.retrieveProxy(PureProxy.NAME) as PureProxy;
  }
  
  //列出Mediator关心的Notification
  override public function listNotificationInterests():Array
  {
   return[
    PureNotify.VIEW_OPEN,
    PureNotify.MEDIATOR_LOGIN_VERIFY,
    PureNotify.LOGIN
   ]
  }
  
  /**
   * 接收来自Model的通知
   * 处理Notification
   * 处理的Notification应该在4、5个之内
   **/
  override public function handleNotification(notification:INotification):void
  {
   switch(notification.getName())
   {
    case PureNotify.VIEW_OPEN:
     proxy.VIEW_OPENFunc(notification.getBody());//继续调用model中的方法
     break;
    case PureNotify.MEDIATOR_LOGIN_VERIFY:
     //this.facade.registerCommand(PureNotify.SIMPLECOMMAND_LOGIN_VERIFY,PureCommand);//注册请求用户信息验证
     this.facade.sendNotification(PureNotify.SIMPLECOMMAND_LOGIN_VERIFY,notification);
    case PureNotify.LOGIN:
     var isSuccess:Boolean=notification.getBody() as Boolean;
     if(isSuccess){
      Alert.show("验证成功");
     }else{
      Alert.show("验证失败");
     }
     break;
   }
  }
 }
}

 

 --------------------LoginVO.as

package mvc.vo
{
 /**
  * 由于是和组件进行绑定所以不需要get和set方法
  **/
 [Bindable]
 public class LoginVO
 {
  public function LoginVO()
  {
  }
  
  public var username:String;
  public var password:String;

 }
}

 

 

---------------------------ApplicationFacade.as

package mvc
{
 import mvc.controller.PureCommand;
 import mvc.controller.StartCommand;
 import mvc.model.PureProxy;
 import mvc.view.PureMediator;
 
 import org.puremvc.as3.interfaces.IFacade;
 import org.puremvc.as3.patterns.facade.Facade;

 /**
  * 由于ApplicationFacade类继承了Facade类,因此ApplicationFacade类将作为整个系统其他角色相互访问通信的核心
  * Facade只能发送Notification不能进行接收
  **/
 public class ApplicationFacade extends  Facade implements IFacade
 {
  public function ApplicationFacade()
  {
   super();//由于子类重写了父类的构造方法,因此在子类构造方法里必须先调用父类的构造方法
  }
  
  //定义主应用程序实例
  public static var main:PureMVCTest;//可以用于*.as文件的调用
  
  //得到ApplicationFacade单例工厂,并且接下来进行初始化
  public static function getInstance():ApplicationFacade{
   if(instance == null) instance = new ApplicationFacade();
   return instance as ApplicationFacade;//必须强制转换
  }
  
  /**
   * 注册Command(控制器),建立Command与Notification之间的映射
   * 当Notification(通知)发出时相关的Command(命令)就会执行
   **/
  override protected function initializeController():void{
   super.initializeController();
   //所有的控制器将都在这里注册
   registerCommand(PureNotify.START,StartCommand);//注册控制器(控制器的启动,用于主应用程序进行启动控制器)
   registerCommand(PureNotify.CMD_OPEN,PureCommand);//登记控制器
   registerCommand(PureNotify.PROXY_LOGIN_VERIFY,PureCommand);//
   registerCommand(PureNotify.SIMPLECOMMAND_LOGIN_VERIFY,PureCommand);//注册请求用户信息验证
  }
  
  /**
   * 初始化Model
   **/
  override protected function initializeModel():void{
   super.initializeModel();
   registerProxy(new PureProxy(PureProxy.NAME));//注册Model
  }
  
  /**
   * 初始化View
   **/
  override protected function initializeView():void{
   super.initializeView()
   registerMediator(new PureMediator(PureMediator.Name));//注册视图
  }
  
  //启动pureMVC,在应用程序中调用此方法,并传递应用程序本身的的引用
  public function startup(app:PureMVCTest):void{
   main=app;
   sendNotification(PureNotify.START,app);//发送通知
  }
  
 }
}

 

--------------------PureNotify.as

package mvc
{
 //定义Notification常量(所有notification的常量必须进行相应的登记,否则无法使用)
 public class PureNotify
 {
  //--------------------------------所有的Mediator发出的通知----------------------------------------
  public static const VIEW_OPEN:String = "VIEW_OPEN";
  
  /**
   * 进行用户信息验证
   **/
  public static const MEDIATOR_LOGIN_VERIFY:String="MEDIATOR_LOGIN_VERIFY";
  
  /**
   * 验证成功后将要进行的操作
   **/
  public static const LOGIN:String="LOGIN";
  
  
  //--------------------------------所有的SimpleCommand发出的通知----------------------------------------
  public static const START:String = "START";
  public static const CMD_OPEN:String = "CMD_OPEN";
  
  /**
   * 请求用户信息验证
   **/
  public static const SIMPLECOMMAND_LOGIN_VERIFY:String="SIMPLECOMMAND_LOGIN_VERIFY";
  
  
  
  //--------------------------------所有的Proxy发出的通知----------------------------------------------
  public static const PROXY_LOGIN_VERIFY:String="PROXY_LOGIN_VERIFY";
 }
}

 

---------------------UserImpl.java

package com.my.test.daoi;

import com.my.test.dao.UserI;
import com.my.test.pojo.User;

public class UserImpl implements UserI{

 public User getUserInfo() {
  User user=new User();
  user.setUsername("wangwei");
  user.setPassword("123456");
  return user;
 }

 public String updateUserInfo() {
  // TODO Auto-generated method stub
  return null;
 }

 

}

 

------------------------web.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" "http://java.sun.com/dtd/web-app_2_3.dtd">
<web-app>

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

    <!-- Http Flex Session attribute and binding listener support -->
    <listener>
        <listener-class>flex.messaging.HttpFlexSession</listener-class>
    </listener>

    <!-- MessageBroker Servlet -->
    <servlet>
        <servlet-name>MessageBrokerServlet</servlet-name>
        <display-name>MessageBrokerServlet</display-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>
   
<!-- begin rds end rds -->
    <servlet>
        <servlet-name>RDSDispatchServlet</servlet-name>
  <display-name>RDSDispatchServlet</display-name>
        <servlet-class>flex.rds.server.servlet.FrontEndServlet</servlet-class>
  <init-param>
   <param-name>useAppserverSecurity</param-name>
   <param-value>false</param-value>
  </init-param>       
        <load-on-startup>10</load-on-startup>
    </servlet>

    <servlet-mapping id="RDS_DISPATCH_MAPPING">
        <servlet-name>RDSDispatchServlet</servlet-name>
        <url-pattern>/CFIDE/main/ide.cfm</url-pattern>
    </servlet-mapping>


    <servlet-mapping>
        <servlet-name>MessageBrokerServlet</servlet-name>
        <url-pattern>/messagebroker/*</url-pattern>
    </servlet-mapping>

    <welcome-file-list>
        <welcome-file>index.html</welcome-file>
        <welcome-file>index.htm</welcome-file>
    </welcome-file-list>

    <!-- for WebSphere deployment, please uncomment -->
    <!--
    <resource-ref>
        <description>Flex Messaging WorkManager</description>
        <res-ref-name>wm/MessagingWorkManager</res-ref-name>
        <res-type>com.ibm.websphere.asynchbeans.WorkManager</res-type>
        <res-auth>Container</res-auth>
        <res-sharing-scope>Shareable</res-sharing-scope>
    </resource-ref>
    -->

</web-app>

 

 

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="userImpl">
  <properties>
   <source>com.my.test.daoi.UserImpl</source>
   <!-- scrop属性的可选值为 application、session和request,指定了远程对象实例作用域 -->
   <scope>session</scope>
   <!-- 通过include-methods属性定义允许访问的方法列表,如果Flex应用试图访问该列表 外的方法就会导致异常,在客户端会调用fault事件 -->
   <include-methods>
                <method name="getUserInfo"/>
            </include-methods>
            <!-- <exclude-methods></exclude- methods>配置设定了Flex客户端可以访问当前类中除了updateUserInfo方法之外的其 他公共方法 -->
            <exclude-methods>
                <method name="updateUserInfo"/>
            </exclude-methods>
  </properties>
 </destination>

</service>

 

 

-----------------jar包需求

在libs目录中必须要有PureMVC_AS3_2_0_4.swc文件

lib下xalan.jar(好像也可不需要)

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值