Passing an array from Java (JSP) to jQuery
I know how to use $.each in my program which is written in Java like this
$.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??
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:
<%@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://jque开发者_开发技巧ry.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!!
Use a JSON library for Java. Then serialize your ArrayList
to JSON:
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.
$(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.
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. :)
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).
var arr1 = [];
arr1[0]="whatever";
arr1[1]="ksj";
$.each( arr1, function(i, l){
alert( "Index #" + i + ": " + l );
});
精彩评论