Gwt-history-class
GWT-履歴クラス
GWTアプリケーションは通常、JavaScriptを実行する単一ページのアプリケーションであり、多くのページを含まないため、ブラウザーはユーザーとアプリケーションとの対話を追跡しません。 ブラウザの履歴機能を使用するには、アプリケーションはナビゲーション可能なページごとに一意のURLフラグメントを生成する必要があります。
GWTは、この状況を処理するための*履歴メカニズム*を提供します。
GWTは、*トークン*という用語を使用します。これは、アプリケーションが特定の状態に戻るために解析できる単なる文字列です。 アプリケーションはこのトークンをブラウザーの履歴にURLフラグメントとして保存します。
たとえば、「pageIndex1」という名前の履歴トークンは次のようにURLに追加されます-
http://www.finddevguides.com/HelloWorldl#pageIndex0
履歴管理ワークフロー
ステップ1-履歴サポートを有効にする
GWT Historyサポートを使用するには、最初に次の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アプリケーションを更新します-アプリケーションの作成_の章-
Step | Description |
---|---|
1 | Create a project with a name HelloWorld under a package com.finddevguides as explained in the GWT - Create Application chapter. |
2 | Modify HelloWorld.gwt.xml, HelloWorld.css, HelloWorldl 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. |
以下は、変更されたモジュール記述子 src/com.finddevguides/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.finddevguides.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/HelloWorldl の内容です。
<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.finddevguides/HelloWorld.java の次の内容を使用して、GWTコードでの履歴管理のデモを行います。
package com.finddevguides.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);
}
}
すべての変更が完了したら、link:/gwt/gwt_create_application [GWT-アプリケーションの作成]の章で行ったように、アプリケーションをコンパイルして開発モードで実行します。 すべてがあなたのアプリケーションでうまくいけば、これは次の結果を生成します-
- 各タブをクリックして、異なるページを選択します。
- 各タブを選択すると、アプリケーションのURLが変更され、#pageIndexがURLに追加されることに注意してください。
- ブラウザの戻るボタンと進むボタンが有効になっていることも確認できます。
- ブラウザの「戻る」ボタンと「進む」ボタンを使用すると、それに応じて異なるタブが選択されます。