UiBinder是一個框架,用於分離用戶界面的功能和視圖。
UiBinder框架允許開發人員將gwt應用程式構建爲HTML頁面,並在其中配置gwt小部件。
UiBinder框架使得與比Java原始碼更熟悉XML、HTML和CSS的UI設計人員的協作更加容易
UIBinder提供了一種定義用戶界面的聲明方式。
UIBinder將程序邏輯與UI分離。
UIBinder與JSP與servlet類似。
UiBinder Workflow
Step 1 - Create UI Declaration XML File
創建一個基於XML/HTML的用戶界面聲明文件。我們在示例中創建了一個Login.ui.xml文件。
<ui:UiBinder xmlns:ui = 'urn:ui:com.google.gwt.uibinder' xmlns:gwt = 'urn:import:com.google.gwt.user.client.ui' xmlns:res = 'urn:with:com.tutorialspoint.client.LoginResources'> <ui:with type = "com.tutorialspoint.client.LoginResources" field = "res"> </ui:with> <gwt:HTMLPanel> ... </gwt:HTMLPanel> </ui:UiBinder>
Step 2 - Use ui:field for Later Binding
使用XML/HTML元素中的ui:field屬性將XML中的ui欄位與JAVA文件中的ui欄位關聯起來,以便以後綁定。
<gwt:Label ui:field = "completionLabel1" /> <gwt:Label ui:field = "completionLabel2" />
Step 3 - Create Java counterpart of UI XML
通過擴展複合窗口小部件,創建基於Java的XML布局對應項。我們在示例中創建了一個Login.java文件。
package com.tutorialspoint.client; ... public class Login extends Composite { ... }
Step 4 - Bind Java UI fields with UiField annotation
使用Login.java中的@UiField注釋指定對應的類成員以綁定到Login.ui.XML中基於XML的欄位
public class Login extends Composite { ... @UiField Label completionLabel1; @UiField Label completionLabel2; ... }
Step 5 - Bind Java UI with UI XML with UiTemplate annotation
指示GWT使用@UiTemplate注釋綁定基於java的組件Login.java和基於XML的布局Login.ui.XML
public class Login extends Composite { private static LoginUiBinder uiBinder = GWT.create(LoginUiBinder.class); /* * @UiTemplate is not mandatory but allows multiple XML templates * to be used for the same widget. * Default file loaded will be <class-name>.ui.xml */ @UiTemplate("Login.ui.xml") interface LoginUiBinder extends UiBinder<Widget, Login> { } ... }
Step 6 - Create CSS File
創建一個外部CSS文件Login.CSS和基於Java的ResourceLoginResources.Java與CSS樣式等效的文件
.blackText { font-family: Arial, Sans-serif; color: #000000; font-size: 11px; text-align: left; } ...
Step 7 - Create Java based Resource File for CSS File
package com.tutorialspoint.client; ... public interface LoginResources extends ClientBundle { public interface MyCss extends CssResource { String blackText(); ... } @Source("Login.css") MyCss style(); }
Step 8 - Attach CSS resource in Java UI Code file.
使用基於Java的widget類的控制項附加外部CSS文件Login.CSS
public Login() { this.res = GWT.create(LoginResources.class); res.style().ensureInjected(); initWidget(uiBinder.createAndBindUi(this)); }
UIBinder Complete Example
這個例子將引導您通過簡單的步驟來展示UIBinder在GWT中的使用。按照以下步驟更新我們在GWT-Create application−一章中創建的GWT應用程式;
Step | Description |
---|---|
1 | Create a project with a name HelloWorld under a package com.tutorialspoint as explained in the GWT - Create Application chapter. |
2 | Modify HelloWorld.gwt.xml, HelloWorld.css, HelloWorld.html and HelloWorld.java as explained below. Keep rest of the files unchanged. |
3 | Compile and run the application to verify the result of the implemented logic. |
下面是修改後的模塊描述符的內容。
<?xml version = "1.0" encoding = "UTF-8"?> <module rename-to = 'helloworld'> <!-- Inherit the core Web Toolkit stuff. --> <inherits name = 'com.google.gwt.user.User'/> <!-- Inherit the default GWT style sheet. --> <inherits name = 'com.google.gwt.user.theme.clean.Clean'/> <!-- Inherit the UiBinder module. --> <inherits name = "com.google.gwt.uibinder.UiBinder"/> <!-- Specify the app entry point class. --> <entry-point class = 'com.tutorialspoint.client.HelloWorld'/> <!-- Specify the paths for translatable code --> <source path ='client'/> <source path = 'shared'/> </module>
下面是修改後的樣式表文件war/HelloWorld.css的內容。
body { text-align: center; font-family: verdana, sans-serif; } h1 { font-size: 2em; font-weight: bold; color: #777777; margin: 40px 0px 70px; text-align: center; }
下面是修改後的HTML宿主文件war/HelloWorld.HTML的內容。
<html> <head> <title>Hello World</title> <link rel = "stylesheet" href = "HelloWorld.css"/> <script language = "javascript" src = "helloworld/helloworld.nocache.js"> </script> </head> <body> <h1>UiBinder Demonstration</h1> <div id = "gwtContainer"></div> </body> </html>
現在創建一個新的UiBinder模板和所有者類(File→new→UiBinder)。
選擇項目的客戶端包,然後將其命名爲Login。保留所有其他默認值。單擊Finish按鈕,插件將創建一個新的UiBinder模板和所有者類。
.blackText { font-family: Arial, Sans-serif; color: #000000; font-size: 11px; text-align: left; } .redText { font-family: Arial, Sans-serif; color: #ff0000; font-size: 11px; text-align: left; } .loginButton { border: 1px solid #3399DD; color: #FFFFFF; background: #555555; font-size: 11px; font-weight: bold; margin: 0 5px 0 0; padding: 4px 10px 5px; text-shadow: 0 -1px 0 #3399DD; } .box { border: 1px solid #AACCEE; display: block; font-size: 12px; margin: 0 0 5px; padding: 3px; width: 203px; } .background { background-color: #999999; border: 1px none transparent; color: #000000; font-size: 11px; margin-left: -8px; margin-top: 5px; padding: 6px; }
package com.tutorialspoint.client; import com.google.gwt.resources.client.ClientBundle; import com.google.gwt.resources.client.CssResource; public interface LoginResources extends ClientBundle { /** * Sample CssResource. */ public interface MyCss extends CssResource { String blackText(); String redText(); String loginButton(); String box(); String background(); } @Source("Login.css") MyCss style(); }
<ui:UiBinder xmlns:ui = 'urn:ui:com.google.gwt.uibinder' xmlns:gwt = 'urn:import:com.google.gwt.user.client.ui' xmlns:res = 'urn:with:com.tutorialspoint.client.LoginResources'> <ui:with type = "com.tutorialspoint.client.LoginResources" field = "res"> </ui:with> <gwt:HTMLPanel> <div align = "center"> <gwt:VerticalPanel res:styleName = "style.background"> <gwt:Label text = "Login" res:styleName = "style.blackText" /> <gwt:TextBox ui:field="loginBox" res:styleName = "style.box" /> <gwt:Label text = "Password" res:styleName = "style.blackText" /> <gwt:PasswordTextBox ui:field = "passwordBox" res:styleName = "style.box" /> <gwt:HorizontalPanel verticalAlignment = "middle"> <gwt:Button ui:field = "buttonSubmit" text="Submit" res:styleName = "style.loginButton" /> <gwt:CheckBox ui:field = "myCheckBox" /> <gwt:Label ui:field = "myLabel" text = "Remember me" res:styleName = "style.blackText" /> </gwt:HorizontalPanel> <gwt:Label ui:field = "completionLabel1" res:styleName = "style.blackText" /> <gwt:Label ui:field = "completionLabel2" res:styleName = "style.blackText" /> </gwt:VerticalPanel> </div> </gwt:HTMLPanel> </ui:UiBinder>
package com.tutorialspoint.client; import com.google.gwt.core.client.GWT; import com.google.gwt.event.dom.client.ClickEvent; import com.google.gwt.event.logical.shared.ValueChangeEvent; import com.google.gwt.uibinder.client.UiBinder; import com.google.gwt.uibinder.client.UiField; import com.google.gwt.uibinder.client.UiHandler; import com.google.gwt.uibinder.client.UiTemplate; import com.google.gwt.user.client.Window; import com.google.gwt.user.client.ui.Composite; import com.google.gwt.user.client.ui.Label; import com.google.gwt.user.client.ui.TextBox; import com.google.gwt.user.client.ui.Widget; public class Login extends Composite { private static LoginUiBinder uiBinder = GWT.create(LoginUiBinder.class); /* * @UiTemplate is not mandatory but allows multiple XML templates * to be used for the same widget. * Default file loaded will be <class-name>.ui.xml */ @UiTemplate("Login.ui.xml") interface LoginUiBinder extends UiBinder<Widget, Login> { } @UiField(provided = true) final LoginResources res; public Login() { this.res = GWT.create(LoginResources.class); res.style().ensureInjected(); initWidget(uiBinder.createAndBindUi(this)); } @UiField TextBox loginBox; @UiField TextBox passwordBox; @UiField Label completionLabel1; @UiField Label completionLabel2; private Boolean tooShort = false; /* * Method name is not relevant, the binding is done according to the class * of the parameter. */ @UiHandler("buttonSubmit") void doClickSubmit(ClickEvent event) { if (!tooShort) { Window.alert("Login Successful!"); } else { Window.alert("Login or Password is too short!"); } } @UiHandler("loginBox") void handleLoginChange(ValueChangeEvent<String> event) { if (event.getValue().length() < 6) { completionLabel1.setText("Login too short (Size must be > 6)"); tooShort = true; } else { tooShort = false; completionLabel1.setText(""); } } @UiHandler("passwordBox") void handlePasswordChange(ValueChangeEvent<String> event) { if (event.getValue().length() < 6) { tooShort = true; completionLabel2.setText("Password too short (Size must be > 6)"); } else { tooShort = false; completionLabel2.setText(""); } } }
package com.tutorialspoint.client; import com.google.gwt.core.client.EntryPoint; import com.google.gwt.user.client.ui.RootPanel; public class HelloWorld implements EntryPoint { public void onModuleLoad() { RootPanel.get().add(new Login()); } }
完成所有更改後,讓我們以開發模式編譯並運行應用程式,就像我們在「gwt-創建應用程式」一章中所做的那樣。如果你的申請一切順利,這將產生以下結果;