- GWT 教程
- GWT - 首頁
- GWT - 概述
- GWT - 環境設定
- GWT - 應用
- GWT - 建立應用
- GWT - 部署應用
- GWT - 使用 CSS 樣式
- GWT - 基本部件
- GWT - 表單部件
- GWT - 複雜部件
- GWT - 佈局面板
- GWT - 事件處理
- GWT - 自定義部件
- GWT - UIBinder
- GWT - RPC 通訊
- GWT - JUnit 整合
- GWT - 除錯應用
- GWT - 國際化
- GWT - 歷史類
- GWT - 書籤支援
- GWT - 日誌框架
- GWT 有用資源
- GWT - 問答
- GWT - 快速指南
- GWT - 有用資源
- GWT - 討論
GWT - 歷史類
GWT 應用通常是執行 JavaScript 的單頁面應用,並且不包含很多頁面,因此瀏覽器不會跟蹤使用者與應用的互動。為了使用瀏覽器的歷史功能,應用應該為每個可導航頁面生成一個唯一的 URL 片段。
GWT 提供了 **歷史機制** 來處理這種情況。
GWT 使用術語 **標記**,它只是一個字串,應用可以解析它以返回到特定狀態。應用會將此標記儲存在瀏覽器的歷史記錄中作為 URL 片段。
例如,名為“pageIndex1”的歷史標記將新增到 URL 中,如下所示:
https://tutorialspoint.tw/HelloWorld.html#pageIndex0
歷史管理工作流程
步驟 1 - 啟用歷史支援
為了使用 GWT 歷史支援,我們必須首先將以下 iframe 嵌入到我們的主機 HTML 頁面中。
<iframe src = "javascript:''" id = "__gwt_historyFrame" style = "width:0;height:0;border:0"></iframe>
步驟 2 - 將標記新增到歷史記錄
以下示例說明了如何將標記新增到瀏覽器歷史記錄
int index = 0;
History.newItem("pageIndex" + index);
步驟 3 - 從歷史記錄中檢索標記
當用戶使用瀏覽器的後退/前進按鈕時,我們將檢索標記並相應地更新我們的應用狀態。
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);
}
}
});
現在讓我們看看歷史類在實際應用中的情況。
歷史類 - 完整示例
此示例將引導您完成簡單的步驟,以演示 GWT 應用的歷史管理。按照以下步驟更新我們在 *GWT - 建立應用* 章節中建立的 GWT 應用:
| 步驟 | 描述 |
|---|---|
| 1 | 在包 *com.tutorialspoint* 下建立一個名為 *HelloWorld* 的專案,如 *GWT - 建立應用* 章節中所述。 |
| 2 | 修改 *HelloWorld.gwt.xml*、*HelloWorld.css*、*HelloWorld.html* 和 *HelloWorld.java*,如下所述。保持其餘檔案不變。 |
| 3 | 編譯並執行應用以驗證已實現邏輯的結果。 |
以下是修改後的模組描述符 **src/com.tutorialspoint/HelloWorld.gwt.xml** 的內容。
<?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>
讓我們使用以下 Java 檔案 **src/com.tutorialspoint/HelloWorld.java** 的內容,我們將用它來演示 GWT 程式碼中的歷史管理。
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 中。
您還可以看到瀏覽器的後退和前進按鈕現在已啟用。
使用瀏覽器的後退和前進按鈕,您將看到不同的選項卡相應地被選中。