最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

How do I pass Java array contents into javascript array? - Stack Overflow

programmeradmin5浏览0评论
<%
    //Retrieving the Server list from dispatcher
    Collection<Server> svr = (Collection<Server>)request.getAttribute("stuff");

    ArrayList<String> serverIds = new ArrayList<String>();

    for(Server i : svr )
      serverIds.add(i.getId());

    String [] svrIds = new String[svr.size()];
    serverIds.toArray(svrIds);

%>

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" ".dtd">
<html>
<head>
<script type="text/javascript">

    var instanceIds = new Array(<%=svrIds%>);

    //test somethings in there
    alert(instanceIds[0]);


</script>
</head>
</html>
<%
    //Retrieving the Server list from dispatcher
    Collection<Server> svr = (Collection<Server>)request.getAttribute("stuff");

    ArrayList<String> serverIds = new ArrayList<String>();

    for(Server i : svr )
      serverIds.add(i.getId());

    String [] svrIds = new String[svr.size()];
    serverIds.toArray(svrIds);

%>

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3/TR/html4/loose.dtd">
<html>
<head>
<script type="text/javascript">

    var instanceIds = new Array(<%=svrIds%>);

    //test somethings in there
    alert(instanceIds[0]);


</script>
</head>
</html>
Share Improve this question asked Aug 25, 2011 at 14:48 stackoverflowstackoverflow 19.6k52 gold badges136 silver badges200 bronze badges 1
  • possible duplicate of Converting a Java ArrayList of strings to a JavaScript array – Jason C Commented Mar 22, 2014 at 15:39
Add a ment  | 

4 Answers 4

Reset to default 3

If you want to use Java code to write the code for a Javascript data structure, probably the simplest way to do it is to use a JSON library for Java. A JSON string can be interpreted as Javascript code.

If you want to use JSON.simple, then there are examples here of how to generate the JSON string:

http://code.google./p/json-simple/wiki/EncodingExamples

In your code, you should be able to do something like this:

var instanceIds = <%= JSONValue.toJSONString(serverIds) %>

You shouldn't need to convert your ArrayList to a Java array. Note that this function is sensitive to what type you pass into it; an array actually won't work in this instance.

Also, to do this you will need to install the JSON.simple JAR file and import org.json.simple.JSONValue in your JSP.

<%
    String[] jArray= new String[2];
    jArray[0]="a";
    jArray[1]="b";

    StringBuilder sb = new StringBuilder();
    for(int i=0;i<jArray.length;i++) 
        sb.append(jArray[i]+",");
%>

<script type="text/javascript">
    temp="<%=sb.toString()%>";
    var strr = new Array();
    strr = temp.split(',','<%=jArray.length%>');

    alert("array: "+strr);
</script>

Both Thomas and Nates answers are good. However, an alternative is to write each into an hidden field then have your js look them up in the DOM.

I'd say you have to use a JavaScript patible string representation of svrIds, like String srvIdsString = "\"element1\",\"element2\"";. To get that, iterate over the array and append "\"" + svrIds[i] + "\"," to your string representation.

Note that the last ma should either be removed again or not added at all (e.g. by skipping it for the last element or adding it to the front and skipping it for the first element).

Example:

StringBuilder idArrayBuilder = new StringBuilder();
for( String id : srvIds ) {
  if( idArrayBuilder.length() > 0 ) {
    idArrayBuilder.append(",");
  }

  idArrayBuilder.append( "\"" ).append(id).append( "\"" );
}
String result = idArrayBuilder.toString();

The rest is up to you :)

发布评论

评论列表(0)

  1. 暂无评论