简体   繁体   English

GWT:如何创建一个新页面

[英]GWT: How to create a new page

I have a GWT MVP application with one page. 我有一页的GWT MVP应用程序。 How can I create a new page and link to it? 如何创建新页面并链接到该页面?

如果您谈论使用GWT进行页面导航,则此链接可能对您有所帮助。

I created a open source, MIT licensed project to ease the page navigation handling in GWT. 我创建了一个由MIT许可的开源项目,以简化GWT中的页面导航处理。 Take a look at the GWT Views project. 看一下GWT Views项目。

Using it you can define a View (a Widget referenced by an unique URL token) using simple Java annotations. 使用它,您可以使用简单的Java注释定义一个View(一个由唯一URL令牌引用的Widget)。 The framework takes care of code-splitting for you, and hides all the boilerplate code. 该框架为您处理代码拆分 ,并隐藏所有样板代码。

Here is an example: 这是一个例子:

@View(Login.TOKEN)
public class Login extends Composite {
//... your code, you can use UIBinder, procedural UI, whatever you like

When using History.newItem(Login.TOKEN) the Login widget will be rendered at the page. 使用History.newItem(Login.TOKEN)Login小部件将在页面上呈现。

There are a lot of common use cases handled by the framework as well, such as ViewContainers, 404 pages, Google Analytics tracking, and user authorization. 该框架还处理许多常见用例,例如ViewContainers,404页面,Google Analytics(分析)跟踪和用户授权。

您可以做一件事,在同一页面上制作不同的布局,并且在特定操作下,您可以隐藏一个布局并显示其他布局或组件。

GWT has support for pages within application via URL fragment identifier , ie http://www.yourhost.vom/main#pagename , where "pagename" is a fragment identifier representing a "page" within your app. GWT通过URL 片段标识符 (即http://www.yourhost.vom/main#pagename支持应用程序中的页面,其中“ pagename”是代表应用程序中“页面”的片段标识符。

  1. Enable history support by adding an iframe to your host page: 通过将iframe添加到您的主机页面来启用历史记录支持

     <iframe src="javascript:''" id="__gwt_historyFrame" style="width:0;height:0;border:0"> </iframe> 
  2. Register a ValueChangeHandler to be notified when history (page) changes. 注册一个ValueChangeHandler以便在历史记录(页面)更改时得到通知。 Within this handler you put a logic that displays the new page. 在此处理程序中,您放置了一个显示新页面的逻辑。

  3. Go to a particular page by calling History.newItem("newpage") 通过调用History.newItem("newpage")转到特定页面

This is what I ended up doing: 这就是我最终要做的事情:

package com.example.client;

import java.util.logging.Logger;

import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.core.shared.GWT;
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.RootPanel;

public class Controller implements EntryPoint {
    private static Controller instance;
    private static final Logger log = Logger.getLogger(Controller.class.getName());

    // I have a feeling GWT does not respect private constructors, or else it uses some other voodoo.
    private Controller(){}

    public static Controller getInstance() {
        if (instance == null) instance = new Controller();
        return instance;
    }

    @Override
    public void onModuleLoad() {
        String token = History.getToken();
        log.info("****************************** token:"+token);
        History.addValueChangeHandler(new ValueChangeHandler<String>() {
            @Override
            public void onValueChange(ValueChangeEvent<String> event) {
                navigate(event.getValue());
            } // onValueChange
        });
        if (token == null || token.length() == 0) History.newItem(Login.TOKEN); // no token
        else navigate(token); // restore app state
    }

    private static void navigate(String token) {
        RootPanel rootPanel = RootPanel.get("gwtApp");
        if (rootPanel.getWidgetCount() > 0) rootPanel.remove(0); // clear the page

        if (Login.TOKEN.equals(token)) {
            Login page = Login.getInstance();
            page.onModuleLoad();
        } else if (MainApp.TOKEN.equals(token)) {
            MainApp page = MainApp.getInstance();
            page.onModuleLoad(); // display the page
//          page.setAuthenticated(true);
//          page.setUsername(email);
        }

    }

} // Controller

In your *.gwt.xml file: 在您的* .gwt.xml文件中:

<entry-point class='com.example.client.Controller' /> 

Now when you want to go to a new page: 现在,当您想转到新页面时:

History.newItem(Login.TOKEN);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM