【问题标题】:GWT - passing Java arrays to JavascriptGWT - 将 Java 数组传递给 Javascript
【发布时间】:2013-04-19 06:07:23
【问题描述】:

我是 GWT 和 Javascript 的新手。

我正在尝试将 java int[] 发送到我的 javascript 函数。我正在使用 gwt-exporter 为我处理处理。我就是这样设置的。

static class gwtExplorerTest implements Exportable {

    @Export("$wnd.handleAnchorClick")
    public static void handleAnchorClick(int param1, int param2 , int[] a1 , int[] a2)
    {
        //process  things here          
    }
}

有人可以帮助我使用 javascript 代码在此处传递我需要的数组吗?我目前拥有的是:

href="javascript:window.handleAnchorClick(" + currentRow + "," + currentColumn + "," + rowVals + "," + colVals + ",") "

作为我的 JS 函数调用,其中 rowVals 和 colVals 是我需要传入的两个数组。它似乎不起作用。有人可以帮帮我吗?

谢谢

【问题讨论】:

  • 我可以从您的代码中推断出,您正在尝试将数组从您的 javascript 传递给您的 java 函数。你不是吗?如果是这样,看看我的回答

标签: java javascript gwt jsni gwt-exporter


【解决方案1】:

您在 java 中的函数是正确的,gwt-exporter 支持这种语法。来自 JS 的调用应该是这样的:

 window.handleAnchorClick(1, 2, [3,4], [5,6])

您的问题是您试图从html 中的href 属性调用导出的方法,并且您使用了错误的语法。

首先,最好使用onClick属性而不是href,因为你不需要javascript:标签,最好防止默认。而且,我宁愿定义一个函数来进行调用以避免语法问题。

<script>
  var currentRow = 1;
  var currentColumn = 2;
  var rowVals = [3,4];
  var colVals = [5,6];

  function mycall() {
    window.handleAnchorClick(currentRow, currentColumn, rowVals, colVals);
  }
</script>

<!-- I prefer this -->
<a href="#" onClick="javascript:mycall()">click</a>

<!-- But this should work as well -->
<a href="#" onClick="window.handleAnchorClick(currentRow,currentColumn,rowVals,colVals)">click</a>

【讨论】:

  • 感谢您的指导。很抱歉回复晚了!。
【解决方案2】:

如果您使用的是 json 字符串,那么我希望您需要在 handleAnchorClick 方法中将参数类型更改为字符串。然后你需要 typecast 为 json。

【讨论】:

    猜你喜欢
    • 2012-09-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-05-25
    相关资源
    最近更新 更多