Escolar Documentos
Profissional Documentos
Cultura Documentos
Developed and taught by well-known author and developer. At public venues or onsite at your location.
For live Ajax and GWT training, please see 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 Courses developed and taught by coreservlets.com experts (edited by Marty) GWT, Java, JSF 2, PrimeFaces, Servlets, JSP, Ajax, jQuery, Spring, Hibernate, RESTful Web Services, Hadoop, Android.
JSF 2, PrimeFaces, servlets/JSP, Ajax, jQuery, Android development, Java 6 or 7 programming, custom mix of topics Ajax courses can concentrate on 1EE Training: http://courses.coreservlets.com/several Customized Java library (jQuery, Prototype/Scriptaculous, Ext-JS, Dojo, etc.) or survey Spring, Hibernate/JPA, EJB3, GWT, Hadoop, SOAP-based and RESTful Web Services
Developed and taught by well-known author and developer. At public venues or onsite at your location. Contact hall@coreservlets.com for details
Overview
Customized Java EE Training: http://courses.coreservlets.com/
GWT, Java, JSF 2, PrimeFaces, Servlets, JSP, Ajax, jQuery, Spring, Hibernate, RESTful Web Services, Hadoop, Android.
Developed and taught by well-known author and developer. At public venues or onsite at your location.
Layout Strategies
HTML-based layout
Write HTML by hand, designate places for individual controls HTML body contains a lot of content Best when GWT is used for
A traditional Web app with some pieces of Ajax-enabled content Complex page where HTML layout does not change You need the HTML content indexed by a search engine
Java-based layout
Attaches main layout panel to HTML <body> element Java uses Panels to build up overall layout
Similar to way LayoutManagers are used in desktop Java
HTML-Based Layout
HTML
<body> Regular Regular Regular Regular </body> HTML <div id="id-1"></div> HTML <div id="id-2"></div> HTML <div id="id-3"></div> HTML
Java
public void onModuleLoad() { SomeWidget w1 = ; RootPanel.get("id-1").add(w1); SomeWidget w2 = ; RootPanel.get("id-2").add(w2); SomeWidget w3 = ; RootPanel.get("id-3").add(w3); }
7
Java-Based Layout
HTML
<body>
<!-- Possibly history iframe, but few or no HTML elements -->
</body>
Java
public void onModuleLoad() { SomePanel mainPanel = ; mainPanel.setVariousProperties(); SomeOtherPanel subPanel = ; SomeWidget w1 = ; subPanel.add(w1); mainPanel.add(subPanel); RootLayoutPanel.get().add(mainPanel); }
8
Even if you manage to get the design into Java, very hard to maintain
9
Advantages of UiBinder
Complex web page layouts using HTML!
Or HTML-like XML Its like JSPs for servlets Much more maintainable
Graphic UI web designers can be involved from initial design through maintanance
Easy to start with regular static HTML page and gradually sprinkle in GWT bindings
Separation of concerns
Aesthetics and functionality of UI no longer mashed together
11
Compile-time check of cross references between XML & Java and even within XML itself (Yee haw!)
Lesson: use regular HTML whenever possible! Goal of UiBinder to make the easier choice the right choice
Tooling support
Eclipse, NetBeans
12
Disadvantages of UiBinder
Causes a bit of a file explosion in your project
There are two files for every component
14
Example: HelloHtmlWorld.ui.xml
<!DOCTYPE ui:UiBinder SYSTEM "http://dl.google.com/gwt/DTD/xhtml.ent"> <ui:UiBinder xmlns:ui="urn:ui:com.google.gwt.uibinder"> <div> Important! This is considered the root element of the UiBinder declaration, even Hello, ♥ though the root element of the XML file is ui:UiBinder. <span ui:field="greetSpan" /> This is what the Java counterpart needs to treat this declaration as, i.e., div element </div> </ui:UiBinder>
Regular XML does not define these XHTML entities. GWT provides them through the DOCTYPE above. This is already baked into the GWT compiler, so dont need to have Internet access for this to work. Yes, this is DTD and XML Schema declaration in the same file! This is completely legal!
This is a bound field. There must be a corresponding member of the same name in HelloHtmlWorld.java . It must have at least package private scope, i.e., can not be private. The type of the corresponding field must be a GWT DOM class that is equivalent to owning tag, i.e., <span>. (SpanElement or its superclass)
15
Example: HelloHtmlWorld.java
... import import import import com.google.gwt.core.client.GWT; com.google.gwt.dom.client.*; com.google.gwt.uibinder.client.*; com.google.gwt.user.client.ui.UIObject;
Only if your root UiBinder element is HTML, i.e., its an HTML-based UiBinder. In this case, the root UiBinder element is div.
public class HelloHtmlWorld extends UIObject { interface HelloHtmlWorldUiBinder extends UiBinder<DivElement, HelloHtmlWorld> {} private static HelloHtmlWorldUiBinder uiBinder = GWT.create(HelloHtmlWorldUiBinder.class); @UiField SpanElement greetSpan; public HelloHtmlWorld(String greeting) { setElement(uiBinder.createAndBindUi(this)); greetSpan.setInnerText(greeting); } }
16
public class HelloHtmlWorld extends UIObject { interface HelloHtmlWorldUiBinder extends UiBinder<DivElement, HelloHtmlWorld> {} private static HelloHtmlWorldUiBinder uiBinder = GWT.create(HelloHtmlWorldUiBinder.class); @UiField SpanElement greetSpan; public HelloHtmlWorld(String greeting) { setElement(uiBinder.createAndBindUi(this)); greetSpan.setInnerText(greeting); } }
17 GWT creates an instance of a factory that can generate the UI and glue it to the owning class, i.e., this class, associating all the fields with @UiField with its declared counterpart.
Creates the root UiBinder type defined in the interface, i.e., DivElement, passing it an instance of an owner class, i.e., HelloHtmlWorld.
18
public class HelloHtmlWorld extends UIObject { interface HelloHtmlWorldUiBinder extends UiBinder<DivElement, HelloHtmlWorld> { } private static HelloHtmlWorldUiBinder uiBinder = GWT.create(HelloHtmlWorldUiBinder.class); @UiField SpanElement greetSpan; public HelloHtmlWorld(String greeting) { setElement(uiBinder.createAndBindUi(this)); greetSpan.setInnerText(greeting); }
19
20
Example: Result
... <fieldset> <legend>Simple UiBinder HTML Example</legend> <div id="uibinder-html"></div> </fieldset> <p/> ...
21
22
Example: HelloWidgetWorld.ui.xml
<!DOCTYPE ui:UiBinder SYSTEM "http://dl.google.com/gwt/DTD/xhtml.ent"> <ui:UiBinder xmlns:ui="urn:ui:com.google.gwt.uibinder" xmlns:g="urn:import:com.google.gwt.user.client.ui"> <g:HTMLPanel> <span style="font-style: italic;">Hello,</span> <g:HTML ui:field="label" /> </g:HTMLPanel> Imports everything in the com.google.gwt.user.client.ui </ui:UiBinder> package and gives it an XML namespace prefix g.
Now, any class in that package can be referenced here with a prefix g. Important! This is considered the root widget of this UiBinder declaration, even though the root element of the XML file is ui:UiBinder. This is what the Java counterpart needs to treat this declaration as, i.e., HTMLPanel
This is a bound field. There must be a corresponding member of the same name in HelloWidgetWorld.java . It must have at least package private scope, i.e., can not be private. The type of the corresponding field must be a GWT widget type that is either HTML or its superclass.
23
Example: HelloWidgetWorld.java
... import com.google.gwt.core.client.GWT; import com.google.gwt.uibinder.client.*; import com.google.gwt.user.client.ui.*;
Only if your root UiBinder element is a GWT Widget, i.e., its a GWT Widget-based UiBinder. In this case, the root UiBinder widget is HTMLPanel, whose superclass is Widget, but we could have used HTMLPanel here just the same. Yes, this IS a custom widget!
public class HelloWidgetWorld extends Composite { interface HelloWidgetWorldUiBinder extends UiBinder<Widget, HelloWidgetWorld> {} private static HelloWidgetWorldUiBinder uiBinder = GWT.create(HelloWidgetWorldUiBinder.class); @UiField HTML label; public HelloWidgetWorld(String greeting) { initWidget(uiBinder.createAndBindUi(this)); label.setText(greeting); } }
24
public class HelloWidgetWorld extends Composite { interface HelloWidgetWorldUiBinder extends UiBinder<Widget, HelloWidgetWorld> {} private static HelloWidgetWorldUiBinder uiBinder = GWT.create(HelloWidgetWorldUiBinder.class); @UiField HTML label; public HelloWidgetWorld(String greeting) { initWidget(uiBinder.createAndBindUi(this)); label.setText(greeting); } }
25 GWT creates an instance of a factory that can generate the UI and glue it to the owning class, i.e., this class, associating all the fields with @UiField with its declared counterpart.
Creates the root UiBinder type defined in the interface, i.e., Widget, passing it an instance of an owner class, i.e., HelloWidgetWorld.
28
Example: Result
... <fieldset> <legend>Simple UiBinder Widget Example</legend> <div id="uibinder-widget"></div> </fieldset> <p/> ...
29
Developed and taught by well-known author and developer. At public venues or onsite at your location.
Summary
UI event handling has a lot of boilerplate code
Create a class that implements SomeHandler interface
Can be done as an anonymous, inner, or standalone Implement the onBlah method in that interface
Disadvantages
GWT created handlers are not sharable with other components
You can still refactor and share the functionality!
33
34
35
37
40
41
Example: UI Event Handling Without UiBinder shortcut: GwtUiBinder1.html & EntryPoint Class
... <fieldset> <legend>UI Event Handling without @UiHandler shortcut</legend> <div id="uibinder-events-without"></div> </fieldset> <p/>
... public class GwtUiBinder1 implements EntryPoint { public void onModuleLoad() { ... HandleEvents1 events1 = new HandleEvents1(); RootPanel.get("uibinder-events-without").add(events1); ... } }
42
43
44
45
Based on the event type, this handler will respond to an onclick event.
46
47
Example: UI Event Handling With UiBinder shortcut: GwtUiBinder1.html & EntryPoint Class
... <fieldset> <legend>UI Event Handling with @UiHandler shortcut</legend> <div id="uibinder-events-with"></div> </fieldset> <p/>
... public class GwtUiBinder1 implements EntryPoint { public void onModuleLoad() { ... HandleEvents2 events2 = new HandleEvents2(); RootPanel.get("uibinder-events-with").add(events2); ... } }
48
49
Wrap-Up
Customized Java EE Training: http://courses.coreservlets.com/
GWT, Java, JSF 2, PrimeFaces, Servlets, JSP, Ajax, jQuery, Spring, Hibernate, RESTful Web Services, Hadoop, Android.
Developed and taught by well-known author and developer. At public venues or onsite at your location.
Summary
Get used to HTML/CSS
Faster & easier to develop web UIs (if you know it) Faster browser performance
In XML, use ui:field to bind a component to its Java counterpart In Java, use @UiField on a class field
Field name must match the ui:field in XML exactly
Java counterpart should extend either UIObject (if pure HTML elements only) or Composite (if using any widgets)
51
Questions?
JSF 2, PrimeFaces, Java 7, Ajax, jQuery, Hadoop, RESTful Web Services, Android, Spring, Hibernate, Servlets, JSP, GWT, and other Java EE training.
Developed and taught by well-known author and developer. At public venues or onsite at your location.