gwt之mvc4g

简介: Mvc4g是一个简单的框架来实现的GWT应用程序的MVC模式。 主要思想 其主要思想是,以减轻开发人员的工作,以单独的视图从模型。该框架是一个XML文件,将允许开发人员告诉视图发射事件时要执行什么样的行动需要配置。

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;
        }
        
}

 

 




 












目录
相关文章
|
7月前
|
XML JSON 前端开发
SAP UI5 MVC 框架实现中的 JSONPropertyBinding
SAP UI5 MVC 框架实现中的 JSONPropertyBinding
40 0
|
存储 设计模式 JSON
Spring MVC: 一种简洁且强大的Web应用框架
这篇文章介绍Spring MVC,Spring MVC是现在基本所有Java程序的主流开发框架,这篇文章主要介绍三部分内容: 实现用户和程序的映射(在浏览器输入URL地址之后,能够在程序中匹配到相应方法)。 服务器端得到用户的请求参数 服务器端将结果返回给用户(前端)
160 0
Spring MVC: 一种简洁且强大的Web应用框架
|
设计模式 前端开发 Java
Java Web(八) MVC和三层架构
今天终于认识到自己的渺小,想起了一句话,不努力机会来了你也抓不住,不要一直想一步登天,一直沉浸在白日梦的美好之中,一步一个脚印,有多大能力做多大的事情,加油把。
153 0
|
前端开发 Java 开发者
|
前端开发 JavaScript Java
|
JSON 前端开发 API
|
缓存 JavaScript 前端开发