The Google Web Toolkit (GWT): The Model-View-Presenter (MVP) Architecture Official MVP Framework



Similar documents
The Google Web Toolkit (GWT): Declarative Layout with UiBinder Basics

Hadoop Streaming coreservlets.com and Dima May coreservlets.com and Dima May

Hadoop Distributed File System (HDFS) Overview

Advanced Java Client API

Apache Pig Joining Data-Sets

Java with Eclipse: Setup & Getting Started

Map Reduce Workflows

Virtual Machine (VM) For Hadoop Training

The Google Web Toolkit (GWT): Overview & Getting Started

Official Android Coding Style Conventions

Managed Beans II Advanced Features

HBase Key Design coreservlets.com and Dima May coreservlets.com and Dima May

HBase Java Administrative API

Android Programming: Installation, Setup, and Getting Started

Building Web Services with Apache Axis2

Android Programming Basics

JHU/EP Server Originals of Slides and Source Code for Examples:

HDFS Installation and Shell

Android Programming: 2D Drawing Part 1: Using ondraw

Application Security

Session Tracking Customized Java EE Training:

2011 Marty Hall An Overview of Servlet & JSP Technology Customized Java EE Training:

Web Applications. Originals of Slides and Source Code for Examples:

What servlets and JSP are all about

Object-Oriented Programming in Java: More Capabilities

Hadoop Introduction coreservlets.com and Dima May coreservlets.com and Dima May

MapReduce on YARN Job Execution

HDFS - Java API coreservlets.com and Dima May coreservlets.com and Dima May

Localization and Resources

Web Applications. For live Java training, please see training courses at

Handling the Client Request: Form Data

Debugging Ajax Pages: Firebug

& JSP Technology Originals of Slides and Source Code for Examples:

For live Java EE training, please see training courses

Enterprise Application Development In Java with AJAX and ORM

OUR COURSES 19 November All prices are per person in Swedish Krona. Solid Beans AB Kungsgatan Göteborg Sweden

Pentesting Web Frameworks (preview of next year's SEC642 update)

An introduction to creating JSF applications in Rational Application Developer Version 8.0

Intruduction to Groovy & Grails programming languages beyond Java

CrownPeak Java Web Hosting. Version 0.20

Building Applications with Protégé: An Overview. Protégé Conference July 23, 2006

An Overview of Servlet & JSP Technology

CHOOSING THE RIGHT HTML5 FRAMEWORK To Build Your Mobile Web Application

Servlet and JSP Filters

Web Frameworks. web development done right. Course of Web Technologies A.A. 2010/2011 Valerio Maggio, PhD Student Prof.

SSC - Web development Model-View-Controller for Java web application development

Architecture Guide Jahia EE v6.1

Rapid Application Development. and Application Generation Tools. Walter Knesel

Framework Adoption for Java Enterprise Application Development

Basic Java Syntax. Slides 2016 Marty Hall,

Case Studies of Running the Platform. NetBeans UML Servlet JSP GlassFish EJB

Budget Event Management Design Document

Credits: Some of the slides are based on material adapted from

Client-side Web Engineering From HTML to AJAX

Preface. Motivation for this Book

GUI and Web Programming

Web Service Caching Using Command Cache

Liferay Enterprise ecommerce. Adding ecommerce functionality to Liferay Reading Time: 10 minutes

Google App Engine f r o r J av a a v a (G ( AE A / E J / )

Progressive Enhancement With GQuery and GWT. Ray Cromwell

Native v HTML5 An Event Planner s Primer

Complete Java Web Development

How To Understand The Architecture Of Java 2Ee, J2Ee, And J2E (Java) In A Wordpress Blog Post

Google Web Toolkit (GWT) Architectural Impact on Enterprise Web Application

White Paper. Java versus Ruby Frameworks in Practice STATE OF THE ART SOFTWARE DEVELOPMENT 1

An introduction to creating Web 2.0 applications in Rational Application Developer Version 8.0

Java (J2SE & J2EE) and Web Development Training Catalog

Controlling Web Application Behavior

The Oracle Fusion Development Platform

The Google Web Toolkit (GWT): JavaScript Native Interface (JSNI)

Google Web Toolkit. Progetto di Applicazioni Software a.a. 2011/12. Massimo Mecella

Mobile Application Development

Google Web Toolkit. Introduction to GWT Development. Ilkka Rinne & Sampo Savolainen / Spatineo Oy

Kotlin for Android Developers

Vaadin Application Tutorial. Building a Simple Address Book

Mobile and Web Applications Developer Track

WINDOWS AZURE EXECUTION MODELS

<Insert Picture Here> Betting Big on JavaServer Faces: Components, Tools, and Tricks

Web and Enterprise Applications Developer Track

Customize Mobile Apps with MicroStrategy SDK: Custom Security, Plugins, and Extensions

Syllabus INFO-GB Design and Development of Web and Mobile Applications (Especially for Start Ups)

Whitepaper. Trans. for Mobile

Structured Content: the Key to Agile. Web Experience Management. Introduction

The Java EE 6 Platform. Alexis Moussine-Pouchkine GlassFish Team

Managing Data on the World Wide-Web

Step One Check for Internet Connection

Choosing a Content Management System (CMS)

Applets, RMI, JDBC Exam Review

Performance Optimization For Operational Risk Management Application On Azure Platform

The end. Carl Nettelblad

Unit Testing with JUnit: A Very Brief Introduction

White Paper On. Single Page Application. Presented by: Yatin Patel

Syllabus INFO-UB Design and Development of Web and Mobile Applications (Especially for Start Ups)

Transcription:

2013 Marty Hall & Yaakov Chaikin The Google Web Toolkit (GWT): The Model-View-Presenter (MVP) Architecture Official MVP Framework (GWT 2.5 Version) Originals of Slides and Source Code for Examples: http://courses.coreservlets.com/course-materials/gwt.html Customized Java EE Training: http://courses.coreservlets.com/ GWT, Java 7 and 8, JSF 2, PrimeFaces, HTML5, Servlets, JSP, Ajax, jquery, Spring, Hibernate, REST, Hadoop, Android. Developed and taught by well-known author and developer. At public venues or onsite at your location. 2013 Marty Hall & Yaakov Chaikin For live Ajax & GWT training, see training courses at http://courses.coreservlets.com/. Taught by the author of Core Servlets and JSP, More Servlets and JSP, and this tutorial. Available at public venues, or customized versions can be held on-site at your organization. Courses developed and taught by Marty Hall JSF 2, PrimeFaces, servlets/jsp, Ajax, jquery, Android development, Java 7 or 8 programming, custom mix of topics Courses Customized available in any state Java or country. EE Training: Maryland/DC http://courses.coreservlets.com/ area companies can also choose afternoon/evening courses. GWT, Courses Java 7 developed and 8, JSF and 2, PrimeFaces, taught by coreservlets.com HTML5, Servlets, JSP, experts Ajax, (edited jquery, by Spring, Marty) Hibernate, REST, Hadoop, Android. Spring, Hibernate/JPA, GWT, Hadoop, HTML5, RESTful Web Services Developed and taught by well-known Contact author hall@coreservlets.com and developer. At public for details venues or onsite at your location.

Topics in This Section Motivation What s wrong with what we already have? Pros and cons Main components of MVP framework Model View ClientFactory Activity Place PlaceHistoryMapper ActivityMapper 6 2013 Marty Hall & Yaakov Chaikin Overview 7 Customized Java EE Training: http://courses.coreservlets.com/ GWT, Java 7 and 8, JSF 2, PrimeFaces, HTML5, Servlets, JSP, Ajax, jquery, Spring, Hibernate, REST, Hadoop, Android. Developed and taught by well-known author and developer. At public venues or onsite at your location.

So What s Wrong with What We Already Had? History management is all over the app Presenters hard code and/or know too much about history Defining the Display (i.e., view) interface inside the presenter doesn t allow for bidirectional communication Impossible to use @UiHandler shortcut with UiBinder View creation means a lot of DOM calls Expensive and slow! If no state is saved in the view, why create new view every time? Just reuse the same view instance. 8 So What s Wrong with What We Already Had? (Continued) Recycling view instances is technically not part of MVP but very useful and fits well into MVP DOM operations are expensive so we try to avoid them Note that this implies that no state should ever be stored in a view Keep the presenters disposable No DOM code resides there, so they are very lightweight and cheap to recreate every time No need to reset their state GWT MVP elegantly abstracts history handling All these are advantages of GWT MVP Framework Besides all the same advantages of MVP in general 9

Disadvantages of GWT MVP All the same as before, but now a REALLY steep learning curve! Can feel a bit like this at first: Bottom line: Give yourself time to get used to all the standard steps 10 From xkcd.com and Randall Munroe. 2013 Marty Hall & Yaakov Chaikin Components of MVP Framework 11 Customized Java EE Training: http://courses.coreservlets.com/ GWT, Java 7 and 8, JSF 2, PrimeFaces, HTML5, Servlets, JSP, Ajax, jquery, Spring, Hibernate, REST, Hadoop, Android. Developed and taught by well-known author and developer. At public venues or onsite at your location.

Main Components of GWT MVP Framework Model Responsible for holding raw data No business logic allowed (just like any model really) Example: public class Customer { // can be Serializable private String firstname, lastname; public Customer(String fname, String lname) { public String getfirstname() { public void setfirstname(string firstname) { 12 Main Components of GWT MVP Framework (Continued) View Responsible for displaying data No business logic allowed Based on a custom standalone interface which defines the methods of this view Inside view interface, we define the Presenter interface for the concrete presenter that will be associated with this view Allows UiBinder to pass through UiHandler methods to the presenter View interface extends iswidget interface that defines aswidget method See next slide for example 13

Main Components of GWT MVP Framework (Continued) Example View interface: public interface HelloView extends iswidget { void setname(string name); void setpresenter(presenter presenter); public interface Presenter { String getname(); 14 Main Components of GWT MVP Framework (Continued) ClientFactory Not technically part of MVP, but very useful to facilitate creation of a view once and only once The rest of the app uses ClientFactory to obtain view references Example: public interface ClientFactory { EventBus geteventbus(); HelloView gethelloview(); 15

Main Components of GWT MVP Framework (Continued) Activity It s a Presenter with bells and whistles built in Has lifecycle methods to hook into when: User is trying to navigate away from this activity Activity s widget (i.e., the view it controls) is removed from user s view Etc. Implements the Presenter interface defined in the associated View interface All business logic goes here Responses to events on the view, server calls, etc. See example on next slide 16 Main Components of GWT MVP Framework (Continued) Activity example: public class HelloActivity extends AbstractActivity implements HelloView.Presenter { private String name; private ClientFactory clientfactory; @Override Equivalent to the go method we had in the Presenter before. public void start(acceptsonewidget container, EventBus eventbus) { HelloView view = clientfactory.gethelloview(); view.setname(name); 17

Main Components of GWT MVP Framework (Continued) 18 Place Encapsulates a navigational state Most Activities have an associated Place Each BlahPlace extends the Place class Usually has a Tokenizer defined as an inner public class Implements PlaceTokenizer<BlahPlace> Knows how to take a URL history token and reconstruct a BlahPlace instance public BlahPlace getplace(string token); Knows how to take a BlahPlace instance and reconstruct a URL history token public String gettoken(blahplace place); A place doesn t need a custom Tokenizer if the only URL data it cares about is the name of the place itself Main Components of GWT MVP Framework (Continued) Place example: public class HelloPlace extends Place { private String name; public HelloPlace(String token) { this.name = token; public String getname() { return name; public static class Tokenizer implements PlaceTokenizer<HelloPlace> { @Override public String gettoken(helloplace place) { @Override public HelloPlace getplace(string token) { 19

Main Components of GWT MVP Framework (Continued) PlaceHistoryMapper A way to let GWT know about your history tokenizers Link between PlaceTokenizers and GWT s history handling mechanism I.e., Link between PlaceTokenizers and PlaceHistoryHandler Example: @WithTokenizers({HelloPlace.Tokenizer.class, public interface MyPlaceHistoryMapper extends PlaceHistoryMapper { 20 Main Components of GWT MVP Framework (Continued) ActivityMapper A way to map each Place to some Activity Remember that Activity implements Presenter interface I.e., it IS a presenter GWT MVP framework uses it to automatically route the app to an Activity Example: public class MyActivityMapper implements ActivityMapper { @Override public Activity getactivity(place place) { if (place instanceof HelloPlace) { return new HelloActivity(); else if (place instanceof ) { 21

Summary: Main Components of GWT MVP Framework Model Responsible for holding raw data (no business logic) View Responsible for displaying data (extends iswidget) Defines BlahView and an associated Presenter interface ClientFactory Holds on to instances of views and other reusable system resources Activity Implements view s Presenter interface and drives all business logic (i.e., it is a presenter) Responds to events on the view, makes server calls, etc. 22 Summary: Main Components of GWT MVP Framework (Cont.) Place Encapsulates a navigational state Defines Tokenizer that knows how to make a Place out of history token and make a URL history token out of Place PlaceHistoryMapper Maps places and their tokenizers in GWT MVP framework s history handling mechanism ActivityMapper Maps each Place to some Activity GWT MVP framework uses it to automatically route to the app to that Activity 23

Some Other GWT MVP Components (Provided by GWT MVP Framework) PlaceController Provided by GWT to programmatically go to a Place, etc. ActivityManager Keeps track of all Activities within the context of one container widget Listens for PlaceChangeRequestEvents and calls the lifecycle methods of Activity to proceed Activity can let the user reject the request by returning a non-null value (a message to the user) in Activity.onMayStop() 24 2013 Marty Hall & Yaakov Chaikin Simple MVP Framework Example 25 Customized Java EE Training: http://courses.coreservlets.com/ GWT, Java 7 and 8, JSF 2, PrimeFaces, HTML5, Servlets, JSP, Ajax, jquery, Spring, Hibernate, REST, Hadoop, Android. Developed and taught by well-known author and developer. At public venues or onsite at your location.

Example Setup Using Google s GWT MVP example app Very simple (easier to understand the architecture) 26 EntryPoint: HelloMVP.java public class HelloMVP implements EntryPoint { private Place defaultplace = new HelloPlace("World!"); private SimplePanel appwidget = new SimplePanel(); public void onmoduleload() { ClientFactory clientfactory = GWT.create(ClientFactory.class); EventBus eventbus = clientfactory.geteventbus(); PlaceController placecontroller = clientfactory.getplacecontroller(); Create ClientFactory using differed binding. 27 Start ActivityManager for the main widget with our ActivityMapper. ActivityMapper activitymapper = new AppActivityMapper(clientFactory); ActivityManager activitymanager = new ActivityManager(activityMapper, eventbus); activitymanager.setdisplay(appwidget);

EntryPoint: HelloMVP.java (continued) Start PlaceHistoryHandler with our PlaceHistoryMapper AppPlaceHistoryMapper historymapper = GWT.create(AppPlaceHistoryMapper.class); PlaceHistoryHandler historyhandler = new PlaceHistoryHandler(historyMapper); historyhandler.register(placecontroller, eventbus, defaultplace); Register GWT MVP PlaceController and the default place (in case no tokens are on the URL or invalid token) RootPanel.get().add(appWidget); historyhandler.handlecurrenthistory(); This is equivalent to History.fireCurrentHistoryState() in GWT MVP framework s API. I.e., it tells the app to start trying to interpret the URL tokens. 28 HelloView.java public interface HelloView extends IsWidget { void setname(string helloname); void setpresenter(presenter listener); public interface Presenter { void goto(place place); 29

HelloViewImpl.java public class HelloViewImpl extends Composite implements HelloView { public void setname(string name) { this.name = name; namespan.setinnertext(name); This view is using UiBinder. @UiHandler("goodbyeLink") void onclickgoodbye(clickevent e) { presenter.goto(new GoodbyePlace(name)); @Override public void setpresenter(presenter presenter) { this.presenter = presenter; 30 HelloPlace.java public class HelloPlace extends Place { private String helloname; public HelloPlace(String token) { this.helloname = token; public String gethelloname() { return helloname; public static class Tokenizer implements PlaceTokenizer<HelloPlace> { public String gettoken(helloplace place) { return place.gethelloname(); 31 public HelloPlace getplace(string token) { return new HelloPlace(token);

AppPlaceHistoryMapper.java @WithTokenizers( { HelloPlace.Tokenizer.class, GoodbyePlace.Tokenizer.class ) public interface AppPlaceHistoryMapper extends PlaceHistoryMapper { Let s GWT know about our Places and Tokenizers. 32 HelloActivity.java public class HelloActivity extends AbstractActivity implements HelloView.Presenter { private ClientFactory clientfactory; private String name; public HelloActivity(HelloPlace place, ClientFactory clientfactory) { this.name = place.gethelloname(); this.clientfactory = clientfactory; public void start(acceptsonewidget containerwidget, EventBus eventbus) { HelloView helloview = clientfactory.gethelloview(); helloview.setname(name); helloview.setpresenter(this); containerwidget.setwidget(helloview.aswidget()); Contains views, eventbus, placecontroller. Retrieve navigational state so the view can be recreated with it. ActivityManager calls start to start this activity s processing. 33

HelloActivity.java (continued) Asks user before stopping activity public String maystop() { return "Please hold on. This activity is stopping."; Method from HelloView interface. public void goto(place place) { clientfactory.getplacecontroller().goto(place); 34 AppActivityMapper.java public class AppActivityMapper implements ActivityMapper { private ClientFactory clientfactory; public AppActivityMapper(ClientFactory clientfactory) { this.clientfactory = clientfactory; public Activity getactivity(place place) { if (place instanceof HelloPlace) { return new HelloActivity((HelloPlace) place, clientfactory); else if (place instanceof GoodbyePlace) { return new GoodbyeActivity((GoodbyePlace) place, clientfactory); return null; If Place can t be mapped to an Activity, stays on the same Activity as before, i.e., does nothing. 35

GoodbyeActivity.java public class GoodbyeActivity extends AbstractActivity { private ClientFactory clientfactory; private String name; public GoodbyeActivity(GoodbyePlace place, ClientFactory clientfactory) { this.name = place.getgoodbyename(); this.clientfactory = clientfactory; No need to implement GoodbyeView interface as GoodbyeView does not need any communication with its associated Activity. Navigational state is passed through the Place instance. public void start(acceptsonewidget containerwidget, EventBus eventbus) { GoodbyeView goodbyeview = clientfactory.getgoodbyeview(); goodbyeview.setname(name); containerwidget.setwidget(goodbyeview.aswidget()); This Activities sole purpose is to set a string on the view. 36 Tested in Production Mode 37

2013 Marty Hall & Yaakov Chaikin Deferred Binding 38 Customized Java EE Training: http://courses.coreservlets.com/ GWT, Java 7 and 8, JSF 2, PrimeFaces, HTML5, Servlets, JSP, Ajax, jquery, Spring, Hibernate, REST, Hadoop, Android. Developed and taught by well-known author and developer. At public venues or onsite at your location. Deferred Binding For ClientFactory Very similar to IoC (Inversion of Control) When you call GWT.create(ClientFactory.class), GWT looks up the implementing class in its app.gwt.xml Example: <!-- Use ClientFactoryImpl by default --> <replace-with class="com.hellomvp.client.clientfactoryimpl"> <when-type-is class="com.hellomvp.client.clientfactory"/> </replace-with> 39 Can use <when-property-is>, specifying user agent (type of browser), i.e. mobile, desktop, etc. Allows to give different view impl for mobile vs desktop

2013 Marty Hall & Yaakov Chaikin Wrap-Up Customized Java EE Training: http://courses.coreservlets.com/ GWT, Java 7 and 8, JSF 2, PrimeFaces, HTML5, Servlets, JSP, Ajax, jquery, Spring, Hibernate, REST, Hadoop, Android. Developed and taught by well-known author and developer. At public venues or onsite at your location. Summary REALLY big learning curve, but very flexible and customizable Still worth it for large scale apps Views define their interface as well as Activities interface (its presenter) Places handle navigational state Place-defined Tokenizer allows conversion from token to Place instance and back Use ClientFactory approach to cache reusable resources in your app Using appname.gwt.xml, use deferred binding to create different views for different browsers, devices, etc. 41

2013 Marty Hall & Yaakov Chaikin Questions? JSF 2, PrimeFaces, Java 7 or 8, Ajax, jquery, Hadoop, RESTful Web Services, Android, HTML5, Spring, Hibernate, Servlets, JSP, GWT, and other Java EE training. Customized Java EE Training: http://courses.coreservlets.com/ GWT, Java 7 and 8, JSF 2, PrimeFaces, HTML5, Servlets, JSP, Ajax, jquery, Spring, Hibernate, REST, Hadoop, Android. Developed and taught by well-known author and developer. At public venues or onsite at your location.