有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java如何将JavaScript API与GWT集成?

我正在尝试使用GWT开发Documentum D2外部小部件。D2提供了一个OpenAjaxHub内置的JavaScript API,用于通信

因此,我尝试将此API导入到公用文件夹中的GWT应用程序中,并将其添加到GWT中。用xml标记脚本

所以,做一些像这样简单的事情

public class WidgetTest implements EntryPoint {

    @Override
    public void onModuleLoad() {
        final RootLayoutPanel rp = RootLayoutPanel.get();

        rp.add(new Label("Hello"));
        init();
    }

    private native void init() /*-{ 
            var d2OpenAjaxHub = new D2OpenAjaxHub();
    }-*/;
}

我得到一个错误:

com。谷歌。gwt。果心客户JavaScriptException:(ReferenceError)@com。卑鄙的。寡妇。客户WidgeTest::init()([]):未定义D2OpenAjaxHub

gwt。xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE module PUBLIC "-//Google Inc.//DTD Google Web Toolkit 2.6.0//EN" "http://google-web-toolkit.googlecode.com/svn/tags/2.6.0/distro-source/core/src/gwt-module.dtd">
<module rename-to="widgetTest">
    <inherits name="com.google.gwt.user.theme.clean.Clean" />
    <inherits name="com.google.gwt.user.User" />

    <script src="OpenAjaxManagedHub-all.js" />
    <script src="D2-OAH.js" />

    <source path="client" />

    <entry-point class="com.vilt.widgetTest.client.WidgetTest" />
</module>

关于D2-OAH。js(我只提取了一点):

D2OpenAjaxHub = function() {

    this.hubClient = null;

    if (typeof(console) == 'undefined')
        console = {};

    if (typeof(console.log) == 'undefined')
        console.log = function(){};

    if (typeof(console.debug) == 'undefined')
        console.debug = console.log;

    if (typeof(console.error) == 'undefined')
        console.error = console.log;


    // ///////////////////
    // CONTEXT / WIDGET INFO
    // ///////////////////

    this.sContextUid = null;
    this.sWidgetType = null;
    this.sWidgetId = null;
    this.bActive = true;

    // ///////////////////
    // EVENTS
    // ///////////////////
    this.registeredChannelIds = new Array();
    this.registeredChannels = new Array();

    this.sStoredChannel = null;
    this.sStoredMessage = null;
    this.fStoredCallback = null;
    this.bStoredMessageConsumed = false;

}

function D2OpenAjaxHub() {
}

有人知道发生了什么事吗


共 (2) 个答案

  1. # 1 楼答案

    首先,必须通过当前窗口$wnd调用方法

    native void myMethod() /*-{
      $wnd.myMethodThatCallsTheJsFile();
    }-*/;
    

    如果仍无法使用此方法进行调试:

    你有没有检查过js是否真的被注入到你的页面?(右键单击结果页面并查看HTML源代码)

    如果是,请尝试将js直接放在页面中(可能在导入js之前调用该方法)