javascript - How could I iterate the values in ArrayList in java from java script? - Stack Overflow

I have stored a bulk of objects in an ArrayList and I have set that in the request. Now I want to retri

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
Add a ment  | 

2 Answers 2

Reset to default 3

You 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条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信