GWT:将UIBinder与HTML模板一起使用。。。如何让它工作?

GWT:将UIBinder与HTML模板一起使用。。。如何让它工作?,gwt,uibinder,Gwt,Uibinder,我目前正在尝试使用GWT的UIBinder功能,但没有成功。我已经阅读了好几次文档,但它没有包含完整的故事。也许你能帮我 以下是我目前所知道的: 1) 我需要创建一个HelloWorld.ui.xml文件,其中包含一些小部件布局。 2) 我需要创建一个对应的HelloWorld类 以下是我找不到的信息: A) 我应该将HelloWorld.ui.xml文件放在哪里,以使GWT能够找到它 B) 如何将HelloWorld组件添加到面板中 文档非常稀少,而且肯定是由对GWT了解得太多的人编写的,新手

我目前正在尝试使用GWT的UIBinder功能,但没有成功。我已经阅读了好几次文档,但它没有包含完整的故事。也许你能帮我

以下是我目前所知道的:

1) 我需要创建一个HelloWorld.ui.xml文件,其中包含一些小部件布局。 2) 我需要创建一个对应的HelloWorld类

以下是我找不到的信息:

A) 我应该将HelloWorld.ui.xml文件放在哪里,以使GWT能够找到它

B) 如何将HelloWorld组件添加到面板中

文档非常稀少,而且肯定是由对GWT了解得太多的人编写的,新手不知道的东西。

a)您需要将
HelloWorld.ui.xml
文件与包含该ui.xml文件逻辑的widget类放在同一个包中。类名应该是
HelloWorld
(为了简单起见,我说您需要使用相同的名称,但是可以通过代码为ui.xml文件使用不同的名称。)

B) 您的
HelloWorld
类应该是扩展小部件的类。就像任何“普通”小部件一样,它可以添加到任何面板中

以下是将
HelloWorld.ui.xml
绑定到
HelloWorld
小部件类中的代码:

public class HelloWorld extends Composite /*or extend any widget you want*/ {
    //This defines an interface that represents this specific HelloWorld.ui.xml file.
    interface MyUiBinder extends UiBinder<Widget, HelloWorld> {}

    // This code is for GWT so it can generate the code from your HelloWorld.ui.xml
    private static MyUiBinder uiBinder = GWT.create(MyUiBinder.class);

    //Constructor
    public HelloWidgetWorld() {
        // This binds the HelloWorld.ui.xml with this widget
        initWidget(uiBinder.createAndBindUi(this));
        ...
    }

    ...
}
public类HelloWorld扩展Composite/*或扩展您想要的任何小部件*/{
//这定义了一个表示此特定HelloWorld.ui.xml文件的接口。
接口MyUiBinder扩展了UiBinder{}
//此代码用于GWT,因此它可以从HelloWorld.ui.xml生成代码
私有静态MyUiBinder=GWT.create(MyUiBinder.class);
//建造师
公共HelloWidgetWorld(){
//这会将HelloWorld.ui.xml与此小部件绑定
initWidget(uiBinder.createAndBindUi(this));
...
}
...
}

此概述解释了所有内容:

  • 将HelloWorld.ui.xml与HelloWorld.java文件放在同一文件夹中
  • 如下所示(摘自上述链接):

    公共类HelloWorld扩展UIObject{//可以改为扩展小部件
    接口HelloWorldUiBinder扩展UiBinder{}
    私有静态HelloWorldUiBinder uiBinder=
    create(HelloWorldUiBinder.class);
    //通过UiBinder创建的div元素
    私有分割元素;
    公共HelloWorld(){
    //createAndBindUi
    divElement=uiBinder.createAndBindUi(这个);
    //现在,您可以将创建的DivElement添加到面板中
    }
    };
    

  • 以下是两个完整的教程和HelloWorld示例:

    1) 对于具有GWT控件的UI活页夹:

    2) 对于具有纯HTML的UI活页夹:
    哎呀!StackOverflow不允许我发布两个超链接。因此,我将在一个单独的答案中发布第二个,或在对此的注释中发布第二个。

    另外,安装,它将完成大部分工作(您只需输入组件名称,它将创建相应的java和ui.xml文件)…:)以下是指向纯HTML示例的链接:
    public class HelloWorld extends UIObject { // Could extend Widget instead
        interface HelloWorldUiBinder extends UiBinder<DivElement, HelloWorld> {}
        private static HelloWorldUiBinder uiBinder =  
              GWT.create(HelloWorldUiBinder.class);
    
        // div element created via UiBinder
        private DivElement divElement;
    
        public HelloWorld() {
            // createAndBindUi 
            divElement = uiBinder.createAndBindUi(this);
            // now you can add created DivElement to your panel
        }
    };