 
- 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 使用 History 类支持浏览器历史记录管理,您可以参考GWT - History Class章节。
GWT 使用术语标记,它只是一个字符串,应用程序可以解析该字符串以返回到特定状态。应用程序会将此令牌作为 URL 片段保存在浏览器的历史记录中。
在GWT - History Class章节中,我们通过编写代码来处理历史记录中令牌的创建和设置。
在本文中,我们将讨论一个特殊的小部件超链接,它自动为我们创建令牌和历史管理,并为应用程序提供书签功能。
书签示例
此示例将引导您完成简单的步骤来演示 GWT 应用程序的书签。
以下步骤更新我们在GWT - 创建应用程序章节中创建的 GWT 应用程序 -
| 步 | 描述 | 
|---|---|
| 1 | 按照GWT - 创建应用程序章节中的说明,在com.tutorialspoint包下创建一个名为HelloWorld的项目。 | 
| 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> Bookmarking 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.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.HorizontalPanel;
import com.google.gwt.user.client.ui.Hyperlink;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.TabPanel;
import com.google.gwt.user.client.ui.VerticalPanel;
public class HelloWorld implements EntryPoint {
   
   private TabPanel tabPanel;
   private void selectTab(String historyToken){
      /*  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);
      }
   }
   /**
    * This is the entry point method.
    */
   public void onModuleLoad() {
      /* create a tab panel to carry multiple pages */  
      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";
      Hyperlink firstPageLink = new Hyperlink("1", "pageIndex0");
      Hyperlink secondPageLink = new Hyperlink("2", "pageIndex1");
      Hyperlink thirdPageLink = new Hyperlink("3", "pageIndex2");
      HorizontalPanel linksHPanel = new HorizontalPanel();
      linksHPanel.setSpacing(10);
      linksHPanel.add(firstPageLink);
      linksHPanel.add(secondPageLink);
      linksHPanel.add(thirdPageLink);		
      /* If the application starts with no history token, 
         redirect to a pageIndex0 */
      String initToken = History.getToken();
      if (initToken.length() == 0) {
         History.newItem("pageIndex0");
         initToken = "pageIndex0";
      }		
      tabPanel.setWidth("400");
      /* add pages to tabPanel*/
      tabPanel.add(firstPage, firstPageTitle);
      tabPanel.add(secondPage,secondPageTitle);
      tabPanel.add(thirdPage, thirdPageTitle);
      /* 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) {
            selectTab(event.getValue());				
         }
      });
      selectTab(initToken);
      VerticalPanel vPanel = new VerticalPanel();
      vPanel.setSpacing(10);
      vPanel.add(tabPanel);
      vPanel.add(linksHPanel);
      /* add controls to RootPanel */
      RootPanel.get().add(vPanel);
   }
} 
准备好完成所有更改后,让我们在开发模式下编译并运行应用程序,就像我们在GWT - 创建应用程序章节中所做的那样。如果您的应用程序一切正常,这将产生以下结果 -
 
- 现在单击 1、2 或 3。您可以注意到选项卡随索引而变化。 
- 您应该注意到,当您单击 1,2 或 3 时,应用程序 url 会发生更改,并且 #pageIndex 会添加到 url 中 
- 您还可以看到浏览器的后退和前进按钮现在已启用。 
- 使用浏览器的后退和前进按钮,您将看到相应地选择不同的选项卡。 
- 右键单击 1、2 或 3。您可以看到打开、在新窗口中打开、在新选项卡中打开、添加到收藏夹等选项。 
- 右键单击 3. 选择添加到收藏夹。将书签另存为第 3 页。 
- 打开收藏夹并选择第 3 页。您将看到选定的第三个选项卡。