GWT JSNI - 如何使用jsni从java代码调用外部javascript

时间:2014-07-06 12:17:26

标签: java gwt jsni

我想在我的gwt应用程序中调用外部javascript函数

当我使用此代码时,它工作正常: 我已导入javaScript,我可以在客户端的firebug中看到它

public static native void showOfflineMap(String pRegionId, String pMapServerHost, String pMapServerPath, String pMapTileLayers, String pFoiLayers,
            double pMapCenterLon, double pMapCenterLat, int pInitZoom, String pNavPanel, boolean pCustJsFunction, int pMapWidth, int pMapHeight,
            String pAdditional) /*-{
                                alert("ff");
                                                $wnd.createPluginMap(
                                                pRegionId,
                                                pMapServerHost,
                                                pMapServerPath,
                                                pMapTileLayers,
                                                pFoiLayers,
                                                pMapCenterLon,
                                                pMapCenterLat,
                                                pInitZoom,
                                                pNavPanel,
                                                pCustJsFunction,
                                                pMapWidth,
                                                pMapHeight,
                                                pAdditional
                                                )


                                                }-*/;

但是当我复发时

alert("ff");

它抛出了这个异常:

Sun Jul 06 16:36:49 GMT+03:30 2014 com.google.gwt.logging.client.LogConfiguration
SEVERE: (TypeError) @ir.dena.avl.client.offlinemap.MapUtil::showOfflineMap(Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;DDILjava/lang/String;ZIILjava/lang/String;)([string: 'offline_map', string: 'http://192.168.10.196:7001', string: '/mapviewer', string: 'mp.LINE_TILE', string: 'NONE', double: 56.0046, double: 32.629365, int: 3, string: 'WEST', bool: false, int: 1000, int: 800, string: 'OVER:MARQUEE:DISTANCE:REDLINE:FOISELECT']): $wnd.createPluginMap is not a functioncom.google.gwt.core.client.JavaScriptException: (TypeError) @ir.dena.avl.client.offlinemap.MapUtil::showOfflineMap(Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;DDILjava/lang/String;ZIILjava/lang/String;)([string: 'offline_map', string: 'http://192.168.10.196:7001', string: '/mapviewer', string: 'mp.LINE_TILE', string: 'NONE', double: 56.0046, double: 32.629365, int: 3, string: 'WEST', bool: false, int: 1000, int: 800, string: 'OVER:MARQUEE:DISTANCE:REDLINE:FOISELECT']): $wnd.createPluginMap is not a function
    at com.google.gwt.dev.shell.BrowserChannelServer.invokeJavascript(BrowserChannelServer.java:249)
    at com.google.gwt.dev.shell.ModuleSpaceOOPHM.doInvoke(ModuleSpaceOOPHM.java:136)
    at com.google.gwt.dev.shell.ModuleSpace.invokeNative(ModuleSpace.java:576)
    at com.google.gwt.dev.shell.ModuleSpace.invokeNativeVoid(ModuleSpace.java:304)
    at com.google.gwt.dev.shell.JavaScriptHost.invokeNativeVoid(JavaScriptHost.java:107)
    at ir.dena.avl.client.offlinemap.MapUtil$.showOfflineMap(MapUtil.java)
    at ir.dena.avl.client.offlinemap.OfflineMapView.createMainPanel(OfflineMapView.java:31)
    at ir.jalal.core.client.view.BaseView.<init>(BaseView.java:71)
    at ir.dena.avl.client.offlinemap.OfflineMapView.<init>(OfflineMapView.java:20)
    at ir.dena.avl.client.ViewFactory$32.onSuccess(ViewFactory.java:354)
    at com.google.gwt.core.client.GWT$1.execute(GWT.java:286)
    at com.google.gwt.core.client.impl.SchedulerImpl$Task$.executeScheduled$(SchedulerImpl.java:50)
    at com.google.gwt.core.client.impl.SchedulerImpl.runScheduledTasks(SchedulerImpl.java:180)
    at com.google.gwt.core.client.impl.SchedulerImpl.flushPostEventPumpCommands(SchedulerImpl.java:345)
    at com.google.gwt.core.client.impl.SchedulerImpl$Flusher.execute(SchedulerImpl.java:78)
    at com.google.gwt.core.client.impl.SchedulerImpl.execute(SchedulerImpl.java:138)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at com.google.gwt.dev.shell.MethodAdaptor.invoke(MethodAdaptor.java:103)
    at com.google.gwt.dev.shell.MethodDispatch.invoke(MethodDispatch.java:71)
    at com.google.gwt.dev.shell.OophmSessionHandler.invoke(OophmSessionHandler.java:172)
    at com.google.gwt.dev.shell.BrowserChannelServer.reactToMessagesWhileWaitingForReturn(BrowserChannelServer.java:338)
    at com.google.gwt.dev.shell.BrowserChannelServer.invokeJavascript(BrowserChannelServer.java:219)
    at com.google.gwt.dev.shell.ModuleSpaceOOPHM.doInvoke(ModuleSpaceOOPHM.java:136)
    at com.google.gwt.dev.shell.ModuleSpace.invokeNative(ModuleSpace.java:576)
    at com.google.gwt.dev.shell.ModuleSpace.invokeNativeObject(ModuleSpace.java:284)
    at com.google.gwt.dev.shell.JavaScriptHost.invokeNativeObject(JavaScriptHost.java:91)
    at com.google.gwt.core.client.impl.Impl.apply(Impl.java)
    at com.google.gwt.core.client.impl.Impl.entry0(Impl.java:356)
    at sun.reflect.GeneratedMethodAccessor88.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at com.google.gwt.dev.shell.MethodAdaptor.invoke(MethodAdaptor.java:103)
    at com.google.gwt.dev.shell.MethodDispatch.invoke(MethodDispatch.java:71)
    at com.google.gwt.dev.shell.OophmSessionHandler.invoke(OophmSessionHandler.java:172)
    at com.google.gwt.dev.shell.BrowserChannelServer.reactToMessages(BrowserChannelServer.java:293)
    at com.google.gwt.dev.shell.BrowserChannelServer.processConnection(BrowserChannelServer.java:547)
    at com.google.gwt.dev.shell.BrowserChannelServer.run(BrowserChannelServer.java:364)
    at java.lang.Thread.run(Unknown Source)

请帮助我,

提前致谢

2 个答案:

答案 0 :(得分:1)

确保您在 createPluginMap之前将脚本与.nocache.js函数相关联,并且不会异步链接它(我的意思是它应该没有{{1} }或<script defer

答案 1 :(得分:0)

我认为它是一种竞争条件,并且当你期望它时,函数createPluginMap不存在。

尝试替换

alert("ff");

alert(''+!!$wnd.createPluginMap);

如果你弄错了&#39;在警报中,这就是原因。 如果是真的,我会尝试考虑不同的答案;)