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