使用maven创建一个简单的gwt工程

(1)文件树
TodoListApp
├── pom.xml
└── src
└── main
├── java
│   └── org
│   └── gwtproject
│   └── tutorial
│   ├── client
│   │   ├── MyPanel.java
│   │   ├── MyPanel.ui.xml
│   │   ├── resources
│   │   │   ├── images
│   │   │   │   └── image1.jpg
│   │   │   └── MyResources.java
│   │   └── TodoList.java
│   └── TodoList.gwt.xml
└── webapp
├── favicon.ico
├── TodoList.css
├── TodoList.html
└── WEB-INF
└── web.xml

(2)创建方法(以2.6.1为例)
下载gwt-2.6.1.zip文件, 解压后将解压后文件的根目录所在路径添加到PATH环境变量中。
运行: webAppCreator -templates maven,sample -out TodoListApp org.gwtproject.tutorial.TodoList
其中webAppCreator是gwt自带的创建项目的工具。

(3)maven项目源码包(解压后使用mvn clean package命令即可生成war包)
 

(4)运行效果



(5)注意事项
------------1-----------------
请注意,*.gwt.xml中<source>标签所指定的path是会被gwt翻译处理的path,如果引用了Resources等,请一定要放在path所在目录或其子目录下,否则会出现错误。
例如:本测试工程中,在MyResources中定义了图片资源,然后在*.ui.xml中使用,
但编译时报如下错误
Invoking generator com.google.gwt.uibinder.rebind.UiBinderGenerator
[INFO] [ERROR] No such type org.gwtproject.tutorial.resources.MyResources: <ui:with field='res' type='org.gwtproject.tutorial.resources.MyResources'> (:5)
[INFO] [ERROR] Errors in 'org/gwtproject/tutorial/client/MyPanel.java'
[INFO] [ERROR] Line 20: Failed to resolve 'org.gwtproject.tutorial.client.MyPanel.MyUiBinder' via deferred binding

(6)关键代码
-----------TodoList.gwt.xml----------
<?xml version="1.0" encoding="UTF-8"?>

<!DOCTYPE module PUBLIC "-//Google Inc.//DTD Google Web Toolkit 2.6.1//EN"
"file:////keycer_workspace/hypersys_work/dayfiles/1117/gwt-2.6.1/gwt-module.dtd">

<module rename-to='todolist'>

<inherits name='com.google.gwt.user.User'/>

<entry-point class='org.gwtproject.tutorial.client.TodoList'/>

<source path='client'/>

</module>

------------TodoList.java-------------
package org.gwtproject.tutorial.client;

import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.user.client.ui.RootPanel;

public class TodoList implements EntryPoint {

public void onModuleLoad() {
MyPanel panel=new MyPanel();
RootPanel.get().add(panel);
}
}

-------------MyPanel.java--------------
package org.gwtproject.tutorial.client;

import com.google.gwt.core.client.GWT;
import com.google.gwt.resources.client.CssResource;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.VerticalPanel;

public class MyPanel extends Composite {
interface MyUiBinder extends UiBinder<VerticalPanel, MyPanel> {
}

interface MyStyles extends CssResource {
String button2();
}

private static final MyUiBinder uiBinder = GWT.create(MyUiBinder.class);

@UiField
MyStyles style;

private VerticalPanel root;

public MyPanel() {
root = uiBinder.createAndBindUi(this);
initWidget(root);

Button button=new Button();
button.setText("DDDDDDDDDD");
button.setStyleName(style.button2());

root.add(button);
}
}

---------------MyPanel.ui.xml---------------
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE ui:UiBinder SYSTEM "http://dl.google.com/gwt/DTD/xhtml.ent">
<ui:UiBinder xmlns:ui='urn:ui:com.google.gwt.uibinder'
xmlns:g='urn:import:com.google.gwt.user.client.ui'>

<ui:with field='res'
type='org.gwtproject.tutorial.client.resources.MyResources' />

<ui:style type="org.gwtproject.tutorial.client.MyPanel.MyStyles">
.button1{
color:red;
border: 1px solid blue;
border-radius: 5px;
}

.label{
color:blue;
border: 1px solid red;
border-radius: 5px;
}

.panel{
border: 1px solid green;
border-radius: 5px;
}

.button2{
color:green;
border: 1px solid blue;
}
</ui:style>

<g:VerticalPanel addStyleNames="{style.panel}" spacing="5">
<g:Button width="200px" height="30px" text="AAAAAA" addStyleNames="{style.button1}"/>
<g:Button width="200px" height="30px" text="BBBBBB" addStyleNames="{style.button1}"/>
<g:Label width="200px" height="30px" text="CCCCCC" addStyleNames="{style.label}"/>
<g:Image width="200px" height="200px" resource="{res.myImage1}"/>
</g:VerticalPanel>
</ui:UiBinder>

---------------MyResources.java------------------
package org.gwtproject.tutorial.client.resources;

import com.google.gwt.resources.client.ClientBundle;
import com.google.gwt.resources.client.ImageResource;
import com.google.gwt.core.client.GWT;

public interface MyResources extends ClientBundle {
public static final MyResources INSTANCE = GWT.create(MyResources.class);

@Source("images/image1.jpg")
ImageResource myImage1();
}

(------------仅供学习交流--------------)

猜你喜欢

转载自blog.csdn.net/zgrjkflmkyc/article/details/53899801