Java 为什么GWT应用程序不显示?
更新:我将为这个问题增加一个悬赏。我在此处将整个项目的源代码添加到GitHub: 我决定将我的Java 为什么GWT应用程序不显示?,java,gwt,mvp,gwt-places,gwt-activities,Java,Gwt,Mvp,Gwt Places,Gwt Activities,更新:我将为这个问题增加一个悬赏。我在此处将整个项目的源代码添加到GitHub: 我决定将我的SimpleModule重命名为WebModule;因此,尽管在我下面的所有代码片段中,GWT模块名为SimpleModule或SimpleModule,但在我最新的代码中,您将看到分别名为WebModule和WebModule的模块,但它们是同一个模块中的1 注意:我知道我可能在这里没有完美的设置,可能有一些根本没有使用的死代码(SimpleModule.css),但这是我的第一个GWT应用程序,我
SimpleModule
重命名为WebModule
;因此,尽管在我下面的所有代码片段中,GWT模块名为SimpleModule
或SimpleModule
,但在我最新的代码中,您将看到分别名为WebModule
和WebModule
的模块,但它们是同一个模块中的1
注意:我知道我可能在这里没有完美的设置,可能有一些根本没有使用的死代码(SimpleModule.css
),但这是我的第一个GWT应用程序,我只想让它启动并运行
我正在试用我的第一个GWT应用程序(2.5.1),并尝试使用推荐的地点和活动框架(用于历史管理)以及基本的MVP架构来获得一个非常简单的UI来显示
为了简单起见,我将所有Java代码放在我的SimpleModule.Java
(入口点)中。一旦这个概念验证开始工作,我将把SimpleModule.java
分解成更多的类
我的目标是在用户进入我的主页(me.example.com)时加载GWT应用程序。因此,我创建了一个simplemoduleimplements EntryPoint
,然后将主机页从SimpleModule.html
重命名为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>
<?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>
<!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>
<?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>
和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);
}
}
}
更新:和SimpleModule.gwt.xml
:
<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>
<?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>
<!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>
<?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>
我现在得到以下例外情况:
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
问题在于:
public class LoginDisplay extends SimplePanel {
private LoginDisplayUiBinder uiBinder = GWT
.create(LoginDisplayUiBinder.class);
public LoginDisplay() {
super();
uiBinder.createAndBindUi(this);
}
}
您创建了小部件,但从未将其添加到面板:
uiBinder.createAndBindUi(this);
尝试:
请注意,您还可以将uiBinder接口放在LoginDisplay
类中,因为这里只需要它(使代码更清晰):
公共类LoginDisplay扩展了SimplePanel{
公共接口LoginDisplayUiBinder扩展UiBinder{}
公共登录显示(){
超级();
logindisplayibinder uiBinder=GWT.create(logindisplayibinder.class);
add(uiBinder.createAndBindUi(this));
}
}
你有app.gwt.xml文件吗?如果是,请张贴。如果不是,那就是原因。更多信息请访问。因为看起来您可能缺少
因为这将毫无问题地加载您的应用程序,所以启动您的html文件,但不会执行任何代码,只留下您提供的未修改的html文件
您如何构建此应用程序 我总是使用相同的项目名称、相同的模块名称和相同的Html页面运行我的应用程序 像这样: 项目:SimpleModule XML:SimpleModule.gwt.XML 其中包括: 最后 SimpleModule.html 您不需要将其重命名为index.html 当gwt编译时,假设您在tomcat中运行它,它不需要index.html,它将SimpleModule.html识别为索引 希望能有所帮助 你应该把你的日志错误,以便更清楚,我建议srart一个新的项目。看看它是如何工作的,并分析你的工作中缺少了什么 也不要忘记de web.xml。1)您是将Eclipse用于此IDE还是其他IDE?当我尝试对ANT文件使用不同的IDE(因此没有插件)时,我无法让GWT正常工作
2) 您将整个GreetingService servlet都放在那里了,您确定这不会给您带来麻烦吗?您正在项目中使用Place和Activity,但您的模块没有继承它们。 将以下行添加到
WebModule.xml
:
<inherits name='com.google.gwt.place.Place'/>
<inherits name='com.google.gwt.activity.Activity'/>
更新
您的新异常是由以下原因引起的:
RootPanel.get().add(loginDisplay);
activityManager.setDisplay(loginDisplay);
您第一次添加登录表单,并将其设置为ActivityManager
的主显示。显示的是面板
参数,该参数在活动的开始方法中给出:
void start(AcceptsOneWidget panel, com.google.gwt.event.shared.EventBus eventBus);
在您的情况下,当您触发页面更改事件(通过使用placeHistoryHandler.handleCurrentHistory();
)时,LoginActivity
将尝试向自身添加LoginDisplay
,从而导致异常
:
panel.setWidget(loginDisplay);
一个简单的修复方法是创建另一个SimplePanel
,并将其设置为主显示:
final SimplePanel mainPanel = new SimplePanel();
RootPanel.get().add(mainPanel);
activityManager.setDisplay(mainPanel);
活动和场所是一个崇高的概念,由于其固有的复杂性,它永远不会流行。试试吧。你再也回不去了。您可以在几分钟内启动并运行MVP示例,GWTP具有A&P所具有的所有历史管理/书签功能,并具有更易于理解和编写代码的API
相信我,GWTP是所有MVP应用程序的必经之路。这可能是宽度、高度等方面的问题。尝试将
、
和主
的宽度和高度设置为100%。使用chrome,您还可以使用F12
来检查应用程序的组件大小。感谢@adenoyelle(+1)-将宽度/高度设置为100%,我将把它放在哪里?我实际上使用的是gwt引导程序
(Twitter引导程序的gwt端口)及其所有CSS文件……因为这只是为了测试问题是否来自这里,您可以直接使用firebug进行测试。当您在github中共享一个192 MB的项目时,很难提供帮助。您应该只共享最基本的必要项目,而不是来自临时、构建等的完整文件!!!!!我只是浪费了5分钟的时间和带宽才意识到下载的大小,然后取消了下载!!!!这个问题需要好好修改一下。这里有很多代码示例,但也链接到整个项目。最后,你似乎把问题改了一半,并寻求帮助,但有一个例外(+
final SimplePanel mainPanel = new SimplePanel();
RootPanel.get().add(mainPanel);
activityManager.setDisplay(mainPanel);