I have stored a bulk of objects in an ArrayList and I have set that in the request. Now I want to retrive the values in the Arraylist from my java script. Please help me with the solution
I have stored a bulk of objects in an ArrayList and I have set that in the request. Now I want to retrive the values in the Arraylist from my java script. Please help me with the solution
Share Improve this question asked Jun 14, 2010 at 11:13 i2ijeyai2ijeya 16.5k18 gold badges66 silver badges73 bronze badges 3- Can you expand on what you mean by you "...have set that in the request"? That doesn't immediately make sense. – T.J. Crowder Commented Jun 14, 2010 at 11:19
- 1 I just setting the list object in the request. – i2ijeya Commented Jun 14, 2010 at 11:21
- I think he means that he'd put it into the request scope attribute. – bezmax Commented Jun 14, 2010 at 11:22
2 Answers
Reset to default 3You can use JSON to facilitate the exchange of information between Java and Javascript. Libraries are available on both ends.
To put the elements of a List
into an array, you can use Collection.toArray
.
You need to serialize them as javascript first. There are 2 ways to do it:
1) Universal way - https://stackoverflow./questions/338586/a-better-java-json-library You just put in your jsp something like this:
<script...>
var myArray = <% JSON.Serialize(myArray) %>;
</script>
2) Fast and dirty:
<script...>
var myArray = [
<c:forEach items="${myArray}" var="item">
{
name = "<c:out value="${item.name}">",
text = "<c:out value="${item.text}">"
},
</c:forEach>
];
</script>
Both will result in Javascript like this, and can be used in JS:
var myArray = [
{
name = "Mike",
text = "Hello world"
},
{
name = "Max",
text = "Hi!"
}
];
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745206059a4616598.html
评论列表(0条)