GWT - History Class

GWT应用程序通常是运行JavaScripts的单页面应用程序,并且不包含大量页面,因此浏览器不会跟踪用户与Application的交互.要使用浏览器的历史记录功能,应用程序应为每个可导航页面生成唯一的URL片段.

GWT提供历史机制来处理这种情况.

GWT使用术语令牌,它只是一个应用程序可以解析以返回特定状态的字符串.应用程序会将此标记作为URL片段保存在浏览器的历史记录中.

例如,名为"pageIndex1"的历史记录标记将添加到URL中,如下所示 :

http://www.it1352.com/HelloWorld.html#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应用程序;

步骤描述
1在包 com.it1352下创建一个名为 HelloWorld 的项目,如下所述在 GWT  - 创建应用程序章节.
2修改 HelloWorld.gwt.xml HelloWorld.css HelloWorld.html HelloWorld.java 如下所述.保持其余文件不变.
3编译并运行应用程序以验证实现的逻辑的结果.

以下是修改后的模块描述符的内容 src/com.it1352/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.IT屋.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.it1352/HelloWorld.java 的内容,我们将使用它来演示历史管理在GWT代码中.

package com.it1352.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  - 创建应用程序章节中做了.如果您的应用程序一切正常,这将产生以下结果 :

GWT History Demo

  • 现在点击每个标签选择不同的页面.

  • 你应该注意,当选择每个标签时,应用网址会被更改并且#pageIndex会被添加到网址.

  • 您还可以看到该浏览器的后退和前进按钮现在已启用.

  • 使用浏览器的后退和前进按钮,您将看到相应的选项被选中.