admin管理员组文章数量:1425195
I have a List of People Objects added to my ModelMap. A person has a name and a date. I know I can access the list by doing the following in the html part of the JSP
${peopleList}
However I want to loop through this list in the JSP in a JavaScript tag, I have tried the following but I am having no luck, and for testing I have just been doing an alert.
var people = "${peopleList}"
alert(people[0].name);
How can I access these properties in JavaScript.
I have a List of People Objects added to my ModelMap. A person has a name and a date. I know I can access the list by doing the following in the html part of the JSP
${peopleList}
However I want to loop through this list in the JSP in a JavaScript tag, I have tried the following but I am having no luck, and for testing I have just been doing an alert.
var people = "${peopleList}"
alert(people[0].name);
How can I access these properties in JavaScript.
Share Improve this question asked Apr 23, 2014 at 3:24 sideshowbobsideshowbob 3266 silver badges23 bronze badges1 Answer
Reset to default 4You are close to the solution, but what you did wrong is a java List
object cannot be converted to a javascript list object directly with var people = "${peopleList}"
. Instead, you can loop through the List
object and create the javascript variables manually using JSTL:
Import the jstl core taglib at the top of the jsp:
<%@ taglib uri="http://java.sun./jsp/jstl/core" prefix="c" %>
Then inside the script tag, create your peopleList:
<script type="text/javascript">
var peopleList = new Array();
<c:forEach items="${peopleList}" var="ppl">
var people = new Object();
people.name = '${ppl.name}';
people.date = '${ppl.date}';
peopleList.push(people);
</c:forEach>
if(peopleList.length > 0){
alert(peopleList[0].name);
}
</script>
本文标签: springAccessing a ModelMap list through JavaScript in JSPStack Overflow
版权声明:本文标题:spring - Accessing a ModelMap list through JavaScript in JSP - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745374482a2655875.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论