1

GWTプロジェクトにhelloJava.HTMLがあり、そのhtmlファイル内にこのjavascriptコードがあります

                        <script type="text/javascript">
function testJavaScript(var input){
    var var1inJS = "Default value";

    alert("Value of Var1 = " + var1inJS);
    var1inJS = input;
    alert("Value of Var1 = " + var1inJS);

    var var2inJS = "Waht is the value of Var2";

    alert("Value of Var2 = " + var2inJS);

}

ここで、このメソッドをonmoduleLoadクラス(つまり、Javaクラス)から呼び出したいと思います。

出来ますか ?

hellojava.htmlファイル

                            <!doctype html>
<!-- The DOCTYPE declaration above will set the     -->
<!-- browser's rendering engine into                -->
<!-- "Standards Mode". Replacing this declaration   -->
<!-- with a "Quirks Mode" doctype is not supported. -->

<html>
  <head>
    <meta http-equiv="content-type" content="text/html; charset=UTF-8">

    <!--                                                               -->
    <!-- Consider inlining CSS to reduce the number of requested files -->
    <!--                                                               -->
    <link type="text/css" rel="stylesheet" href="HelloJSNI.css">

    <!--                                           -->
    <!-- Any title is fine                         -->
    <!--                                           -->
    <title>Web Application Starter Project</title>

    <!--                                           -->
    <!-- This script loads your compiled module.   -->
    <!-- If you add any GWT meta tags, they must   -->
    <!-- be added before this line.                -->
    <!--                                           -->
    <script type="text/javascript" language="javascript" src="hellojsni/hellojsni.nocache.js"></script>

    <script type="text/javascript">
    function testJavascript(var input){
        window.jsniAlert();
        var var1inJS = "Default value";

        alert("Value of Var1 = " + var1inJS);
        var1inJS = input;
        alert("Value of Var1 = " + var1inJS);

        var var2inJS = "Waht is the value of Var2";

        alert("Value of Var2 = " + var2inJS);

    }

    function callJava(){

    }
    </script>
  </head>

  <!--                                           -->
  <!-- The body can have arbitrary html, or      -->
  <!-- you can leave the body empty if you want  -->
  <!-- to create a completely dynamic UI.        -->
  <!--                                           -->
  <body>

    <!-- OPTIONAL: include this if you want history support -->
    <iframe src="javascript:''" id="__gwt_historyFrame" tabIndex='-1' style="position:absolute;width:0;height:0;border:0"></iframe>

    <!-- RECOMMENDED if your web app will not function without JavaScript enabled -->
    <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>

    <h1>JSNI EXAMPLE</h1>

    <table align="center">
      <tr>
        <td colspan="2" style="font-weight:bold;"></td>        
      </tr>
      <tr>
        <td id="nameFieldContainer"></td>
        <td id="sendButtonContainer"></td>
      </tr>
      <tr>
        <td colspan="2" style="color:red;" id="errorLabelContainer"></td>
      </tr>
    </table>
  </body>
</html>
4

2 に答える 2

2

はい。これはおおよそあなたが望むものです:

public static native void callJavascript() /*-{
    $wnd.testJavaScript("hello");
}-*/;

次に、Javaコードでこのメソッドを呼び出します。callJavascript();

JSNIに関するGoogleのドキュメントを読む: https ://developers.google.com/web-toolkit/doc/latest/DevGuideCodingBasicsJSNI

于 2012-04-07T02:58:26.573 に答える
0

Travisの関数構文は正しく、問題は呼び出し元の関数の名前であり、JSスクリプトが正しくありません。

これが訂正です

  1. 関数宣言にエラーがありvarます。引数には使用しないでください。

  2. 最初の行を削除しますwindow.jsniAlert();

  3. 2番目の関数の宣言を次のように修正しfunction callJava(){ますfunction callJava(){

コードは次のとおりです(JSで関数を呼び出しましょうtestJSnative)。

<script type="text/javascript">
function testJSnative(input){
    //window.jsniAlert();
    var var1inJS = "Default value";

    alert("Value of Var1 = " + var1inJS);
    var1inJS = input;
    alert("Value of Var1 = " + var1inJS);

    var var2inJS = "What is the value of Var2";

    alert("Value of Var2 = " + var2inJS);

}

function callJava(){

}
</script>

クラス実装の例EntryPoint

  public void onModuleLoad() {

    Button jsniButton = new Button("call JS");
    jsniButton.addClickHandler(new ClickHandler() {

      @Override
      public void onClick(ClickEvent event) {

        callJavascript();
      }
    });
    RootPanel.get().add(jsniButton);
  } 

  public native void callJavascript() /*-{
    $wnd.testJSnative("hello");
  }-*/;

これがお役に立てば幸いです。

于 2012-04-07T13:16:04.140 に答える