3

更新:我即将为这个问题添加一个赏金。我在这里将整个项目的源代码添加到 GitHub:

https://github.com/doctrang/gwt-activities-places-mvp-example

我决定将我的重命名SimpleModuleWebModule;因此,尽管在我下面的所有代码片段中,GWT 模块都被命名为SimpleModuleor simplemodule,但在我的最新代码中,您将看到模块分别命名为WebModulewebmodule- 但它们是相同的 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.htmlto重命名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
4

6 回答 6

3

问题在这里:

public class LoginDisplay extends SimplePanel {
    private LoginDisplayUiBinder uiBinder = GWT
        .create(LoginDisplayUiBinder.class);

    public LoginDisplay() {
        super();

        uiBinder.createAndBindUi(this);
    }
}

您创建了您的小部件,但从未将其添加到面板中:

uiBinder.createAndBindUi(this);

尝试 :

this.add(uiBinder.createAndBindUi(this));

请注意,您也可以将 uiBinder 接口放在LoginDisplay类中,因为它只在此处需要(使您的代码更清晰):

public class LoginDisplay extends SimplePanel {

  public interface LoginDisplayUiBinder extends UiBinder<Widget, LoginDisplay> {}

  public LoginDisplay() {
    super();

    LoginDisplayUiBinder uiBinder=GWT.create(LoginDisplayUiBinder.class);
    this.add(uiBinder.createAndBindUi(this));
  }
}
于 2013-06-26T13:30:51.917 回答
2

您在项目中使用PlaceActivity,但您的模块没有继承它们。将以下行添加到WebModule.xml

<inherits name='com.google.gwt.place.Place'/>
<inherits name='com.google.gwt.activity.Activity'/>

UiTemplate此外,如果视图模板与其封闭类的名称不同,则必须将注释添加到 UiBinder 接口。在您的情况下,GWTWebModule.ui.xml在尝试创建LoginDisplayUiBinder.

@UiTemplate("LoginDisplay.ui.xml")
public interface LoginDisplayUiBinder extends UiBinder<Widget, LoginDisplay> {}

更新

您的新异常是由以下原因引起的:

RootPanel.get().add(loginDisplay);
activityManager.setDisplay(loginDisplay);

您首次添加登录表单并将其设置为您的ActivityManager. 显示是panel在它们的 start 方法中为您的活动提供的参数:

void start(AcceptsOneWidget panel, com.google.gwt.event.shared.EventBus eventBus);

在您的情况下,当您触发页面更改事件(通过使用placeHistoryHandler.handleCurrentHistory();)时,LoginActivity将尝试添加LoginDisplay到自身导致Exception

panel.setWidget(loginDisplay);

您可以做的一个简单修复是创建另一个SimplePanel并将其设置为主显示:

final SimplePanel mainPanel = new SimplePanel();
RootPanel.get().add(mainPanel);
activityManager.setDisplay(mainPanel);
于 2013-07-06T19:42:44.257 回答
0

你有你的app.gwt.xml 文件吗?如果是这样,请发布它。如果不是,那就是原因。更多关于它的信息请参见 GWT 文档。因为看起来你可能缺乏<entry-point class='com.myapp.MyEntryPointClass' />

因为这将毫无问题地加载您的应用程序,所以启动您的 html 文件,但是不会执行任何代码,留下您提供的未修改的 html 文件。

你如何构建这个应用程序?

于 2013-06-26T15:27:17.083 回答
0

我总是使用相同的项目名称、相同的模块名称和相同的 Html 页面运行我的应用程序。

像这样:

项目:简单模块

XML:SimpleModule.gwt.xml

包含这个:

最后

简单模块.html

您不需要将其重命名为 index.html。

当 gwt 编译时,假设您在 tomcat 中运行它,它不需要 index.html,它会将 SimpleModule.html 识别为索引。

希望它有所帮助。

你应该把你的日志错误写得更清楚,我建议你创建一个新项目。看看它是如何工作的,并分析你的缺失什么。

也不要忘记 de web.xml。

于 2013-07-03T23:00:44.557 回答
0

1)您是在使用 Eclipse 还是其他 IDE?当我尝试对 ANT 文件使用不同的 IDE(所以没有插件)时,我无法让 GWT 正常工作。

2)您将整个 GreetingService servlet 留在了那里,您确定这不会给您带来一些麻烦吗?

于 2013-07-06T15:03:21.573 回答
0

活动与场所是一个崇高的概念,由于其固有的复杂性,它永远不会起飞。请尝试gwtp(GWT 平台)。你永远不会回去。您可以在几分钟内启动并运行有效的 MVP 示例,并且 GWTP 具有 A&P 拥有的所有历史管理/书签以及更易于理解和编写代码的 API。

相信我,GWTP 是所有 MVP 应用程序的必经之路。

于 2013-07-09T12:15:38.830 回答