I have an arraylist of type User which looks like:
console.log([User [id=1, firstName= Sagar, lastName= Dafle, address= Address street 33 S, 3rd street, city San Jose, state California, zip 95113, title= Software Engineer], User [id=2, firstName= Vikas, lastName= Dafle, address= Address street 33 S, 3rd street, city San Jose, state California, zip 95113, title= MBA]]);
Now, I wish to iterate over this list of size 2 and print the user.id and user.firstname . However, when I do so , I get the first record printed twice as:
{1,Sagar} {1,Sagar}
Note that the expected output is:
{1,Sagar} {2,Vikas}
My loop to iterate over the list is :
userlistlength = ${phone.userList.size()};
//console.log(${phone.userList});
console.log("userlistlength "+userlistlength); //prints 2
if(userlistlength>0){
document.write("User List: \n");
for( var counter = 0; counter < userlistlength ; counter++){
console.log("Counter "+counter); //prints 0 and 1
id = '${phone.userList.get(counter).id}';
firstname = '${phone.userList.get(counter).firstname}';
document.write("{"+id+","+firstname+"}\n");
document.write("\n");
}
}
Note that I receive the phone object from my spring controller as a model attribute. Any idea where am I going wrong ? Thanks a lot.
As @Bergi suggested, It is a bad practise to use script tag in JSP pages. I instead used JSP forEach loop and it worked.
Code:
<c:forEach items="${phone.userList}" var="phoneuserobj">
<tr>
<td>${phoneuserobj.id}</td>
<td>${phoneuserobj.firstname}</td>
</tr>
</c:forEach>
for( var counter = 0; counter < userlistlength ; counter++){
console.log("Counter "+counter); //prints 0 and 1
id = '${phone.userList.get(counter+1).id}';
firstname = '${phone.userList.get(counter+1).firstname}';
document.write("{"+id+","+firstname+"}\n");
document.write("\n");
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.