Thursday, July 9, 2009

GWT and MVP (Model View Presenter) Pattern

After watching the google wave presentation at Google I/O, I changed my mind about the GWT (Google Web Toolkit) and I've been studying it hard. From the technical view, GWT is amazing and it's getting better on each new release. I was searching for best practices using GWT and I found this presentation: "GWT App Architecture Best Practices - Ray Ryan". Ray Ryan gives a lot of tips on how to create a well-structured and testable application using GWT. On the first thought I found it a little bit complex: when you deal with a simple screen it's easy to understand, but when you have to deal with a more complex screen, things get way more complex.

Ray Ryan presents the MVP (Passive View) pattern as a good solution, and it is. I spent many hours trying to figure out a way to implement the communication between the presenters. After this hard work I found - in my opinion - a good way to implement it, and I decided to share it with the world. I decided to share this knowledge as a sample application. You can find this application on

I'm not very experienced in this pattern so you might find some bugs or wrong concepts. The idea is to talk about it, to share knowledge. Feel free to change the source code, send me e-mails, add issues, etc.

I'm looking forward to your replies.


Chad Skinner said...

I know this is probably basic and I am overlooking something, but In the source code for the MailPresenterImpl you have the following code:

private void doIssueUpdated(Issue issue) {
BaseView displayView = displayPresenter.get().showIssue(issue);

where is the method .get() on displayPresenter defined?

Jeff said...


This is part of the DI handling. The specific .get() you are asking about is actually on

Some good intros are here, and here.

Basically it is finding a concrete class that implements IssueDisplayPresenter; then instantiates it, and gives you the reference.

Jeff said...

I am curious about the appropriate level of MVP-granularity. I have done most of my GUI-esque programming in JFace/SWT with eclipse plug-in programming.

This lends itself to a certain level of granularity. For example the IssueDisplayWidget and IssueEditWidget are what may be compared to Views in the eclipse world. They are composite with some buttons, and one or more lower level widgets that map to some data.

For the actual lower level components though there is still need for some MVC/MVP concern. For example I could add an IssueListWidget, a presenter, and events wired appropriately to display this new widget. The question I have is that if I were to have some buttons and a table - should I have IssueListWidget handle the eventBus interaction and just use regular old attached listeners to update between IssueListWidget and the underlying table? Or is there any need to make the table have its own presenter and events?

I would be curious to hear thoughts on pros and cons on various ways this could be handled.

Kango_V said...

I'm in this situation as well. I'm wondering how far down the widget stack to take this.

Just getting to grips with gwt-presenter which is an excellent starting point.

Same said...

Looks VERY interesting.
Thanks for the show how.

Same said...

Hi, Im trying to use retrograde analysis to analyze how you set your code up. I have it working in eclipse, but am a bit confused still.
Is it possible to javadoc comments added to the project?
Like for example, If i hover over HandlerManagerProvider, i get the basic javadoc
Author: (Eduardo S. Nunes)
But no actual info about the class.
This would be really helpful to the many many people im SURE are going to be checking this out in the near future.



brendan said...

Did you manage to get history / PlaceManager working when you used gwt-presenter?

On of the things Ray Ryan said was get history sorted out early.

Eduardo S. Nunes said...

Sorry about the late to answer your comments. I didn't receive any e-mail from blogger about the comments so I've just read them today. About gwt-presenter, I'm not using it, it was just a test. I did some modifications in my implementation. I will post these modifications as soon as possible. I will try to do it today.

pas said...

I just want to say thank you for the awesome work!

Gary S said...

Yes, thank you. I just demoed a ui based on gwt-mvp. The most loosely coupled UI I've ever written. Once I learned to import the correct Display. Once I figured out how to do something coding was easy.

How does the event bus handle multiple registrations of the same event/handler? Are there duplicates?

Araminos said...

Hello, everyone is interested in printable and readable version of Google IO about MVP, please look

Álvaro said...

Eduadro, have you had a look at this similar project?

What do you think about it?


Same said...

Hi, I just set up a simplified version of the mvp-sample project. Mine is very simple, with 2 different views and buttons attached to each view that allow it to swith from one view to another.

What would happen if one of the buttons didnt just switch the view, but did a RPC call, but some json data back to populate a table. How do I make the current view(composite) react to these?

See im confused because the sample project the Handlers are registered to the mainpresenterImpl. How would I access the current presenters bound view's widgets to manipulate them?

This kinda falls in line with Jeff and Kango's questions I think, and they didnt get answered.

Please help.



ps: Great work by the way.

Same said...

My other question follows along the lines of Brendens (most import question of mine is right before this one so answer that one first)
PlaceManager, were do you (view, presenter, or presenterImpl) Register the History?



(see ? before this one and thanks again)

shambhu said...

I am trying to use
it is throwing null pointer exception. I used like this

@UiField TextBox materialWeight;
private DoubleWidgetBinder dblMaterialWeight = new DoubleWidgetBinder(0.0,materialWeight);
public HasValue materialWeight() {
System.out.println("dblMaterialWeight : "+dblMaterialWeight);
return dblMaterialWeight;
out put:- dblMaterialWeight : (null handle)

jerzy said...


thank you for the gwt-mvp-example!
But I have a troubles with implemeting it :) For example I have a grid with user names and column with button "edit user". How can I expose from display these buttons as HasClickHandler() ?

Eduardo S. Nunes said...

You can't use HasClickHandlers, you will have to implement a specific Handler/Event that informs you about the click and some id which identifies the selected user.

jerzy said...

I have simple solving of my problem:
I display constructor I create button's array with getter and put these buttons to the grid. In presenter I register these button as HasClickHandler and use their btnId.

But it seems that it's not the best idea

Mikael KROK said...

Thank you for your work,

actually, it is hard to find clear information about MVP implementation. Especially when you try to do more than a tutorial page.

I am trying right now to implements MVP and I am facing some problems I can't figure out how to resolve them.

I have developped 3 differents Presenters independantly, now I am trying to call the second in an DialogBox from the first... that is my problem right now, maybe when I'll crakc your code I will be able to adapt it to my problem. :p :p