javascript 将数组从 Java (JSP) 传递给 jQuery

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6528234/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-25 20:57:57  来源:igfitidea点击:

Passing an array from Java (JSP) to jQuery

javascriptjqueryjsp

提问by user651521

I know how to use $.each in my program which is written in Java like this

我知道如何在像这样用 Java 编写的程序中使用 $.each

$.each( ['a','b','c'], function(i, l){ 
   alert( "Index #" + i + ": " + l ); 
 });

what about If I want to pass arraylist or string array from my java program to the JQuery any one knows how??

如果我想将 arraylist 或字符串数​​组从我的 java 程序传递到 JQuery,有人知道怎么做吗?

I wrote

我写

String[] arr1=new String[2];
arr1[1]="whatever";
arr1[2]="ksj";
$.each( arr1, function(i, l){ 
   alert( "Index #" + i + ": " + l );
 }

but it does not work?

但它不起作用?



Update:: I have jsp page which contains part of java as follows:

更新::我有jsp页面,其中包含java的一部分,如下所示:

<%@page import="java.lang.reflect.Array"%>
<%@page import="java.util.ArrayList"%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>JSP Page</title>

        <style>
            .highlight { background-color: yellow }
        </style>
    </head>
    <body>
        <h1>Hello World!</h1>
        <%  ArrayList wordslist = new ArrayList();
      wordslist.add("Hello");
      wordslist.add("again");

     String[] arr1=new String[wordslist.size()];
      for (int i=0; i<wordslist.size();i++)
      {
     arr1[i]=wordslist.get(i).toString();
        }%>



        <a href="http://jquery.com/">jQuery</a>

        <p> Hello again and again</p>
          <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.5/jquery.min.js"></script>
       <script src="jquery.highlight-3.js"></script>
       <script>

        $(document).ready(function(){
       alert( arr1 );     
      var arr=new Array();
      arr.

$.each( arr, function(i, l){ 
   alert( "Index #" + i + ": " + l );
 }
)


     });

       </script> 
    </body>
</html>

The problem is wordslist arry is dynamic and I want to pass it to the JQuery ?? How can I do that without using inline embedded java please help Is there a way to pass wordslist to arr array in the Jquery!!! This really make me disappointed!!

问题是 wordslist arry 是动态的,我想将它传递给 JQuery ?? 如何在不使用内联嵌入式 Java 的情况下做到这一点,请帮助有没有办法将 wordlist 传递给 Jquery 中的 arr 数组!!!这真的让我很失望!!

回答by Felix Kling

Use a JSON library for Java. Then serialize your ArrayListto JSON:

使用JavaJSON 库。然后将您的序列化为ArrayListJSON:

ArrayList wordslist = new ArrayList();
wordslist.add("Hello");
wordslist.add("again");

String json = (new JSONArray(wordslist)).toString();

Echo the JSON text at the appropriate place in your JavaScript code. E.g.

在 JavaScript 代码中的适当位置回显 JSON 文本。例如

$(document).ready(function(){    
    var arr = <%= json %>;
    $.each( arr, function(i, l){ 
        alert( "Index #" + i + ": " + l );
    });
});

I'm actually not sure whether this is the right JSP syntax for printing a variable. Please correct if not.

我实际上不确定这是否是用于打印变量的正确 JSP 语法。如果不是,请更正。

回答by Adam Terlson

Well your code is formatted wrong, for starters. You're missing the closing bit. Second, indexes are zero-based. Third, you created the array wrong.

好吧,对于初学者来说,您的代码格式错误。你错过了结束点。其次,索引是从零开始的。第三,您创建的数组错误。

var arr1 = new Array();
arr1[0]="whatever";
arr1[1]="ksj";
$.each( arr1, function(i, l){ 
   alert( "Index #" + i + ": " + l );
}); //Missed the );

Overall, I recommend writing JavaScript rather than Java when using jQuery. Just a tip. :)

总的来说,我建议在使用 jQuery 时编写 JavaScript 而不是 Java。只是一个提示。:)

回答by Karl Knechtel

Javascript variables are not declared with a type, and you cannot ask for an array of String, because Javascript only has one kind of array, which holds anything. Also, make sure your brackets balance. Finally, array indices in Javascript start at 0, just like in most other programming languages (including Java, which you seem to be getting confused with - but Java and Javascript have nothing to do with each other, despite the names).

Javascript 变量不是用类型声明的,你不能要求一个字符串数组,因为 Javascript 只有一种数组,它可以容纳任何东西。另外,请确保您的括号平衡。最后,Javascript 中的数组索引从 0 开始,就像在大多数其他编程语言中一样(包括 Java,您似乎对此感到困惑 - 但 Java 和 Javascript 彼此无关,尽管名称不同)。

var arr1 = [];
arr1[0]="whatever";
arr1[1]="ksj";
$.each( arr1, function(i, l){ 
   alert( "Index #" + i + ": " + l );
});