View
The visual: View
The second piece of the MVP architectural pattern is the View. Its purpose is to separate the application logic from the "display" logic. By implementing the UI this way, multiple views can be used to represent the same application (ie: different views for different countries, different platforms, etc).
The views in GWTP aren't much different from GWT widgets, except that they talk to a presenter. So the syntax remains pretty similar to GWT's. The preferred way of using complex views in GWT would be with UiBinder. A view can be defined by a declarative syntax in a .ui.xml
file. Let's continue the previous example as seen here.
SimpleView.ui.xml
SimpleViewImpl.java
The presenter will be omitted for now.
The first things to notice here are the extends
and implements
on the class. In GWTP, views have to extend an implementation of the View
interface. There are multiple implementations available, but let's keep it simple with the regular ViewImpl
. Also, a view should implement a Presenter's view contract. As mentioned in the Presenter section, a Presenter should declare the interface of its view, so this view implements its Presenter's view interface.
If the view uses the UiBinder (like here), it should declare its UiBinder's mapping. That's what the Binder
interface is for. The result of binder.createAndBindUi(this)
is passed to the initWidget method. In GWTP, when it is possible, it is preferred to inject an instance by constructor using dependency injection.
Last updated
Was this helpful?