更新:我即将为这个问题添加一个赏金。我在这里将整个项目的源代码添加到 GitHub:
https://github.com/doctrang/gwt-activities-places-mvp-example
我决定将我的重命名SimpleModule
为WebModule
;因此,尽管在我下面的所有代码片段中,GWT 模块都被命名为SimpleModule
or simplemodule
,但在我的最新代码中,您将看到模块分别命名为WebModule
和webmodule
- 但它们是相同的 1!
注意:我知道我在这里可能没有完美地设置东西,并且可能有一些SimpleModule.css
根本没有使用的死代码(等),但这是我的第一个 GWT 应用程序,我只想得到这个东西启动并运行!
我正在尝试我的第一个 GWT 应用程序(2.5.1),并尝试使用推荐的 Places & Activities 框架(用于历史管理)以及基本的 MVP 架构来显示一个非常简单的 UI。
为简单起见,我已将所有 Java 代码放在我的SimpleModule.java
(入口点)中。一旦我得到这个概念验证工作,我将分解SimpleModule.java
成更多的类。
我的目标是在用户访问我的主页 (me.example.com) 时加载 GWT 应用程序。正因为如此,我创建了一个SimpleModule implements EntryPoint
,然后将主机页面从SimpleModule.html
to重命名index.html
(这样当用户访问 me.example.com 或 me.example.com/index.html 时,他们会拉下SimpleModule
)。
我的WAR目录结构:
war/
hosts/
simplemodule/
SimpleModule.css
WEB-INF/
classes/
lib/
deploy/
web.xml
simplemodule/
css/
font/
gwt/
img/
js/
prettify/
clear.cache.gif
hosted.html
simplemodule.nocache.js
img/
mylogo.jpg
index.html
并且index.html
:
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<script type="text/javascript" language="javascript" src="simplemodule/simplemodule.nocache.js"></script>
</head>
<body>
<iframe src="javascript:''" id="__gwt_historyFrame" tabIndex='-1' style="position:absolute;width:0;height:0;border:0"></iframe>
<noscript>
<div style="width: 22em; position: absolute; left: 50%; margin-left: -11em; color: red; background-color: white; border: 1px solid red; padding: 4px; font-family: sans-serif">
Your web browser must have JavaScript enabled in order for this application to display correctly.
</div>
</noscript>
</body>
</html>
并且web.xml
:
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" version="2.5" xmlns="http://java.sun.com/xml/ns/javaee">
<servlet>
<servlet-name>greetServlet</servlet-name>
<servlet-class>com.myapp.server.GreetingServiceImpl</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>greetServlet</servlet-name>
<url-pattern>/simplemodule/greet</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>remoteLogging</servlet-name>
<servlet-class>com.google.gwt.logging.server.RemoteLoggingServiceImpl</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>remoteLogging</servlet-name>
<url-pattern>/simplemodule/remote_logging</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
</welcome-file-list>
</web-app>
并且SimpleModule.java
:
public class SimpleModule implements EntryPoint {
private EventBus eventBus = new SimpleEventBus();
private PlaceController placeController = new PlaceController(eventBus);
private PlaceHistoryMapper placeHistoryMapper;
private PlaceHistoryHandler placeHistoryHandler;
private Place defaultPlace;
private ActivityMapper activityMapper;
private ActivityManager activityManager;
private LoginDisplay loginDisplay = new LoginDisplay();
@Override
public void onModuleLoad() {
bootstrap();
RootPanel.get().add(loginDisplay);
activityManager.setDisplay(loginDisplay);
placeHistoryHandler.register(placeController, eventBus, defaultPlace);
placeHistoryHandler.handleCurrentHistory();
}
private void bootstrap() {
placeHistoryMapper = new PlaceHistoryMapper() {
@Override
public String getToken(Place arg0) {
return "home";
}
@Override
public Place getPlace(String arg0) {
return defaultPlace;
}
};
placeHistoryHandler = new PlaceHistoryHandler(placeHistoryMapper);
defaultPlace = new Place() {};
activityMapper = new ActivityMapper() {
@Override
public Activity getActivity(Place arg0) {
return new LoginActivity();
}
};
activityManager = new ActivityManager(activityMapper, eventBus);
}
public class LoginDisplay extends SimplePanel {
private LoginDisplayUiBinder uiBinder = GWT
.create(LoginDisplayUiBinder.class);
public LoginDisplay() {
super();
uiBinder.createAndBindUi(this);
}
}
public interface LoginDisplayUiBinder extends UiBinder<Widget, LoginDisplay> {
// ???
}
public class LoginActivity extends AbstractActivity {
@Override
public void start(AcceptsOneWidget panel,
com.google.gwt.event.shared.EventBus eventBus) {
panel.setWidget(loginDisplay);
}
}
}
并且LoginDisplay.ui.xml
:
<!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"
xmlns:b="urn:import:com.github.gwtbootstrap.client.ui">
<g:HTMLPanel>
<img src="img/mylogo.jpg" />
<hr/>
<form action="doStuff" method="post" class="form-horizontal"
id="someForm" accept-charset="utf-8">
<div class="control-group">
<label for="username" class="control-label">
Username:
</label>
<div class="controls">
<input name="username" type="text" value="" id="username"/>
</div>
</div>
<div class="control-group">
<label for="password" class="control-label">
Password:
</label>
<div class="controls">
<input name="password" type="password" value="" id="password"/>
</div>
</div>
<div class="control-group">
<div class="controls">
<input type="button" class="btn-danger" value="Login"/>
</div>
</div>
</form>
</g:HTMLPanel>
</ui:UiBinder>
更新:和SimpleModule.gwt.xml
:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE module PUBLIC "-//Google Inc.//DTD Google Web Toolkit 2.5.1//EN"
"http://google-web-toolkit.googlecode.com/svn/tags/2.5.1/distro-source/core/src/gwt-module.dtd">
<module rename-to='simplemodule'>
<inherits name='com.google.gwt.user.User'/>
<!-- Configure logging. -->
<inherits name="com.google.gwt.logging.Logging"/>
<set-property name="gwt.logging.logLevel" value="FINEST"/>
<set-property name="gwt.logging.enabled" value="TRUE"/>
<set-property name="gwt.logging.consoleHandler" value="ENABLED"/>
<set-property name="gwt.logging.developmentModeHandler" value="DISABLED" />
<set-property name="gwt.logging.popupHandler" value="DISABLED" />
<set-property name="gwt.logging.systemHandler" value="DISABLED" />
<set-property name="gwt.logging.firebugHandler" value="DISABLED" />
<set-property name="gwt.logging.simpleRemoteHandler" value="DISABLED" />
<!-- GWT-Bootstrap. -->
<inherits name ="com.github.gwtbootstrap.Bootstrap"/>
<inherits name='com.google.gwt.user.theme.clean.Clean'/>
<entry-point class='com.myapp.client.SimpleModule'/>
<source path='client'/>
<source path='shared'/>
</module>
服务器启动后的网页源:
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<script type="text/javascript" language="javascript" src="simplemodule/simplemodule.nocache.js"></script>
</head>
<body height="100%" width="100%">
<iframe src="javascript:''" id="__gwt_historyFrame" tabIndex='-1' style="position:absolute;width:0;height:0;border:0"></iframe>
<noscript>
<div style="width: 22em; position: absolute; left: 50%; margin-left: -11em; color: red; background-color: white; border: 1px solid red; padding: 4px; font-family: sans-serif">
Your web browser must have JavaScript enabled in order for this application to display correctly.
</div>
</noscript>
</body>
</html>
当我在 Dev 模式下运行它时(在 上调用 Java 的 Ant 目标com.google.gwt.dev.DevMode
),我得到 DevMode 工具启动而没有错误。当我选择“启动应用程序”时,我得到一个空白网页。当我打开 Firebug 并四处寻找错误时,我看不到任何.
我的设置有什么问题???为什么我看不到一个带有登录字段的简单图像徽标?提前致谢!
编辑:我的最新更新。我遵循@Raphael 的建议(继承 Place 和 Activity 模块,并将@UITemplate
注释添加到LoginDisplayUiBinder
),我得到标签“Hello,GWT!” 打印到我的浏览器!!!然后我修改了我的onModuleLoader()
方法,如下所示:
@Override
public void onModuleLoad() {
bootstrap();
// RootPanel.get().add(new Label("Hello, GWT!"));
RootPanel.get().add(loginDisplay);
activityManager.setDisplay(loginDisplay);
// Connect the PlaceController to the EventBus, and set the
// defaultPlace as our first place in history.
placeHistoryHandler.register(placeController, eventBus, defaultPlace);
placeHistoryHandler.handleCurrentHistory();
}
现在我得到以下异常:
onModuleLoad() threw an exception
Exception while loading module com.dummylandapp.client.WebModule. See Development Mode for details.
java.lang.reflect.InvocationTargetException
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:616)
at com.google.gwt.dev.shell.ModuleSpace.onLoad(ModuleSpace.java:406)
at com.google.gwt.dev.shell.OophmSessionHandler.loadModule(OophmSessionHandler.java:200)
at com.google.gwt.dev.shell.BrowserChannelServer.processConnection(BrowserChannelServer.java:526)
at com.google.gwt.dev.shell.BrowserChannelServer.run(BrowserChannelServer.java:364)
at java.lang.Thread.run(Thread.java:679) Caused by: com.google.web.bindery.event.shared.UmbrellaException: Exception caught: Exception caught: (HierarchyRequestError) @com.google.gwt.dom.client.Node::appendChild(Lcom/google/gwt/dom/client/Node;)([JavaScript object(15)]): Node cannot be inserted
at the specified point in the hierarchy
at com.google.web.bindery.event.shared.SimpleEventBus.doFire(SimpleEventBus.java:203)
at com.google.web.bindery.event.shared.SimpleEventBus.fireEvent(SimpleEventBus.java:88)
at com.google.gwt.place.shared.PlaceController.goTo(PlaceController.java:156)
at com.google.gwt.place.shared.PlaceHistoryHandler.handleHistoryToken(PlaceHistoryHandler.java:192)
at com.google.gwt.place.shared.PlaceHistoryHandler.handleCurrentHistory(PlaceHistoryHandler.java:118)
at com.dummylandapp.client.WebModule.onModuleLoad(WebModule.java:66) ... 9 more Caused by: com.google.gwt.event.shared.UmbrellaException: Exception caught: (HierarchyRequestError) @com.google.gwt.dom.client.Node::appendChild(Lcom/google/gwt/dom/client/Node;)([JavaScript object(15)]): Node cannot be inserted
at the specified point in the hierarchy
at com.google.gwt.activity.shared.ActivityManager.onPlaceChange(ActivityManager.java:168)
at com.google.gwt.place.shared.PlaceChangeEvent.dispatch(PlaceChangeEvent.java:70)
at com.google.gwt.place.shared.PlaceChangeEvent.dispatch(PlaceChangeEvent.java:1)
at com.google.gwt.event.shared.GwtEvent.dispatch(GwtEvent.java:1)
at com.google.web.bindery.event.shared.EventBus.dispatchEvent(EventBus.java:40)
at com.google.web.bindery.event.shared.SimpleEventBus.doFire(SimpleEventBus.java:193) ... 14 more