gwt之mvc4g

Mvc4g一个简单的框架来实现GWT应用程序MVC模式

主要思想

其主要思想是,以减轻开发人员工作以单独的视图从模型该框架一个XML文件,允许开发人员告诉视图发射事件时要执行什么样的行动需要配置

框架如何工作

具体步骤如下图

事件
创建活动的视图控制器事件包含两部分信息

    执行的动作名称
    对象传递行动

UserBean user = new UserBean();

user.setName("John Smith");

new Event("CreateUser", user);

 控制器
控制器接收事件根据事件动作名称执行行动
例如,如果您有以下事件触发

Event e =newEvent("doOperation","+");

以下Mvc4g配置文件

<actionname="doOperation"class="com.mvc4g.example.client.OperationAction"/>

然后控制器调用以下行动实例

com.mvc4g.example.client.OperationAction

创建一个视图,你需要

    实施com.mvc4g.client.ViewInterface
    一个默认的构造

视图可以创建事件和火灾控制器调用的handleEvent函数

Event e =newEvent("doOperation","+");
controller
.handleEvent(e);  

详细的示例

Action

package com.mvc4g.example.client;



import com.mvc4g.client.ActionInterface;

import com.mvc4g.client.Controller;



public class OperationAction implements ActionInterface {

        

        private int value = 0;



        @Override

        public void execute(Controller controller, Object form) {



                //Execute action

                String operation = (String) form;

                if("+".equals(operation)){

                        value++;

                }

                else if("-".equals(operation)){

                        value--;

                }

                

                //Update the view

                ((SimpleCalculatorView)controller.getView("mainView")).updateScreen(Integer.toString(value));



        }



}

 view

package com.mvc4g.example.client;



import com.google.gwt.event.dom.client.ClickEvent;

import com.google.gwt.event.dom.client.ClickHandler;

import com.google.gwt.user.client.ui.Button;

import com.google.gwt.user.client.ui.Composite;

import com.google.gwt.user.client.ui.HorizontalPanel;

import com.google.gwt.user.client.ui.RootPanel;

import com.google.gwt.user.client.ui.TextBox;

import com.google.gwt.user.client.ui.VerticalPanel;

import com.mvc4g.client.Controller;

import com.mvc4g.client.Event;

import com.mvc4g.client.ViewInterface;



public class SimpleCalculatorView extends Composite implements ViewInterface {

        

        private Controller controller = null;

        private TextBox screen = new TextBox();

        

        public SimpleCalculatorView(){

                

                screen.setWidth("50px"); screen.setEnabled(false); screen.setText("0");

                

                Button add = new Button("+1");

                add.addClickHandler(new ClickHandler(){

                        @Override

                        public void onClick(ClickEvent event) {

                                //Create and fire event to the controller

                                Event e = new Event("doOperation", "+");

                                controller.handleEvent(e);                              

                        }                       

                });

                

                Button less = new Button("-1");

                less.addClickHandler(new ClickHandler(){

                        @Override

                        public void onClick(ClickEvent event) {

                                //Create and fire event to the controller

                                Event e = new Event("doOperation", "-");

                                controller.handleEvent(e);                              

                        }                       

                });

                

                HorizontalPanel buttons = new HorizontalPanel();

                buttons.add(add); buttons.add(less);

                

                VerticalPanel mainPanel = new VerticalPanel();

                mainPanel.add(screen);mainPanel.add(buttons);

                

                RootPanel.get().add(mainPanel);

                

        }

        

        public void updateScreen(String value){

                screen.setText(value);

        }



        @Override

        public void setController(Controller controller) {

                this.controller = controller;

        }

        

}

 控制层

要创建一个动作,你需要

    实施com.mvc4g.client.ActionInterface
    一个默认的构造

执行一个动作控制器调用的动作执行功能它在这个功能,你需要的代码行为行动更新视图,动作可以检索控制器控制器调用getView功能感谢为了获取正确的观点行动需要视图的名称

例如如果你有以下调用

controller.getView("mainView")

以下Mvc4g配置文件

<viewname="mainView"class="com.mvc4g.example.client.SimpleCalculatorView"/>

您将检索有以下几种观点实例

com.mvc4g.example.client.SimpleCalculatorView

Action

package com.mvc4g.example.client;



import com.mvc4g.client.ActionInterface;

import com.mvc4g.client.Controller;



public class OperationAction implements ActionInterface {

        

        private int value = 0;



        @Override

        public void execute(Controller controller, Object form) {



                //Execute action

                String operation = (String) form;

                if("+".equals(operation)){

                        value++;

                }

                else if("-".equals(operation)){

                        value--;

                }

                

                //Update the view

                ((SimpleCalculatorView)controller.getView("mainView")).updateScreen(Integer.toString(value));



        }



}

 view

package com.mvc4g.example.client;



import com.google.gwt.event.dom.client.ClickEvent;

import com.google.gwt.event.dom.client.ClickHandler;

import com.google.gwt.user.client.ui.Button;

import com.google.gwt.user.client.ui.Composite;

import com.google.gwt.user.client.ui.HorizontalPanel;

import com.google.gwt.user.client.ui.RootPanel;

import com.google.gwt.user.client.ui.TextBox;

import com.google.gwt.user.client.ui.VerticalPanel;

import com.mvc4g.client.Controller;

import com.mvc4g.client.Event;

import com.mvc4g.client.ViewInterface;



public class SimpleCalculatorView extends Composite implements ViewInterface {

        

        private Controller controller = null;

        private TextBox screen = new TextBox();

        

        public SimpleCalculatorView(){

                

                screen.setWidth("50px"); screen.setEnabled(false); screen.setText("0");

                

                Button add = new Button("+1");

                add.addClickHandler(new ClickHandler(){

                        @Override

                        public void onClick(ClickEvent event) {

                                //Create and fire event to the controller

                                Event e = new Event("doOperation", "+");

                                controller.handleEvent(e);                              

                        }                       

                });

                

                Button less = new Button("-1");

                less.addClickHandler(new ClickHandler(){

                        @Override

                        public void onClick(ClickEvent event) {

                                //Create and fire event to the controller

                                Event e = new Event("doOperation", "-");

                                controller.handleEvent(e);                              

                        }                       

                });

                

                HorizontalPanel buttons = new HorizontalPanel();

                buttons.add(add); buttons.add(less);

                

                VerticalPanel mainPanel = new VerticalPanel();

                mainPanel.add(screen);mainPanel.add(buttons);

                

                RootPanel.get().add(mainPanel);

                

        }

        

        public void updateScreen(String value){

                screen.setText(value);

        }



        @Override

        public void setController(Controller controller) {

                this.controller = controller;

        }

        

}

 

 




 












你可能感兴趣的:(mvc)