GWT應用程式通常是運行JavaScripts的單頁應用程式,不包含很多頁面,因此瀏覽器不會跟蹤用戶與應用程式的交互。若要使用瀏覽器的歷史記錄功能,應用程式應爲每個可導航頁面生成唯一的URL片段。
GWT提供歷史機制來處理這種情況。
GWT使用一個術語token這只是一個字符串,應用程式可以解析它以返回特定的狀態。應用程式將在瀏覽器的歷史記錄中將此令牌保存爲URL片段。
例如,一個名爲「pagindex1」的歷史標記將被添加到URL中,如下所示−
http://www.tutorialspoint.com/HelloWorld.html#pageIndex0
History Management Workflow
Step 1 - Enable History support
爲了使用GWT歷史支持,我們必須首先在宿主HTML頁面中嵌入以下iframe。
<iframe src = "javascript:''" id = "__gwt_historyFrame" style = "width:0;height:0;border:0"></iframe>
Step 2 - Add token to History
下面的示例統計了如何將令牌添加到瀏覽器歷史記錄中
int index = 0; History.newItem("pageIndex" + index);
Step 3 - Retrive token from History
當用戶使用瀏覽器的後退/前進按鈕時,我們將檢索令牌並相應地更新我們的應用程式狀態。
History.addValueChangeHandler(new ValueChangeHandler<String>() { @Override public void onValueChange(ValueChangeEvent<String> event) { String historyToken = event.getValue(); /* parse the history token */ try { if (historyToken.substring(0, 9).equals("pageIndex")) { String tabIndexToken = historyToken.substring(9, 10); int tabIndex = Integer.parseInt(tabIndexToken); /* select the specified tab panel */ tabPanel.selectTab(tabIndex); } else { tabPanel.selectTab(0); } } catch (IndexOutOfBoundsException e) { tabPanel.selectTab(0); } } });
現在讓我們看看歷史課的實際情況。
History Class - Complete Example
本例將通過簡單的步驟演示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'/> <!-- 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> <iframe src = "javascript:''"id = "__gwt_historyFrame" style = "width:0;height:0;border:0"></iframe> <h1> History Class Demonstration</h1> <div id = "gwtContainer"></div> </body> </html>
package com.tutorialspoint.client; import com.google.gwt.core.client.EntryPoint; import com.google.gwt.event.logical.shared.SelectionEvent; import com.google.gwt.event.logical.shared.SelectionHandler; import com.google.gwt.event.logical.shared.ValueChangeEvent; import com.google.gwt.event.logical.shared.ValueChangeHandler; import com.google.gwt.user.client.History; import com.google.gwt.user.client.ui.HTML; import com.google.gwt.user.client.ui.RootPanel; import com.google.gwt.user.client.ui.TabPanel; public class HelloWorld implements EntryPoint { /** * This is the entry point method. */ public void onModuleLoad() { /* create a tab panel to carry multiple pages */ final TabPanel tabPanel = new TabPanel(); /* create pages */ HTML firstPage = new HTML("<h1>We are on first Page.</h1>"); HTML secondPage = new HTML("<h1>We are on second Page.</h1>"); HTML thirdPage = new HTML("<h1>We are on third Page.</h1>"); String firstPageTitle = "First Page"; String secondPageTitle = "Second Page"; String thirdPageTitle = "Third Page"; tabPanel.setWidth("400"); /* add pages to tabPanel*/ tabPanel.add(firstPage, firstPageTitle); tabPanel.add(secondPage,secondPageTitle); tabPanel.add(thirdPage, thirdPageTitle); /* add tab selection handler */ tabPanel.addSelectionHandler(new SelectionHandler<Integer>() { @Override public void onSelection(SelectionEvent<Integer> event) { /* add a token to history containing pageIndex History class will change the URL of application by appending the token to it. */ History.newItem("pageIndex" + event.getSelectedItem()); } }); /* add value change handler to History this method will be called, when browser's Back button or Forward button are clicked and URL of application changes. */ History.addValueChangeHandler(new ValueChangeHandler<String>() { @Override public void onValueChange(ValueChangeEvent<String> event) { String historyToken = event.getValue(); /* parse the history token */ try { if (historyToken.substring(0, 9).equals("pageIndex")) { String tabIndexToken = historyToken.substring(9, 10); int tabIndex = Integer.parseInt(tabIndexToken); /* select the specified tab panel */ tabPanel.selectTab(tabIndex); } else { tabPanel.selectTab(0); } } catch (IndexOutOfBoundsException e) { tabPanel.selectTab(0); } } }); /* select the first tab by default */ tabPanel.selectTab(0); /* add controls to RootPanel */ RootPanel.get().add(tabPanel); } }
完成所有更改後,讓我們以開發模式編譯並運行應用程式,就像我們在「gwt-創建應用程式」一章中所做的那樣。如果你的申請一切順利,這將產生以下結果;
現在單擊每個選項卡以選擇不同的頁面。
您應該注意,當選擇每個選項卡時,應用程式url會更改,並且會將pageIndex添加到url中。
您還可以看到瀏覽器的後退和前進按鈕現在已啓用。
使用瀏覽器的後退和前進按鈕,您將看到相應地選擇了不同的選項卡。