Populating JavaScript Array from JSP List
Posted
by tkeE2036
on Stack Overflow
See other posts from Stack Overflow
or by tkeE2036
Published on 2010-06-14T20:25:33Z
Indexed on
2010/06/15
0:32 UTC
Read the original article
Hit count: 465
Ok so perhaps someone can help me with a problem I'm trying to solve. Essentially I have a JSP page which gets a list of Country objects (from the method referenceData() from a Spring Portlet SimpleFormController, not entirely relevant but just mentioning in case it is). Each Country object has a Set of province objects and each province and country have a name field:
public class Country {
private String name;
private Set<Province> provinces;
//Getters and setters
}
public class Province {
private String name;
//Getters and setters
}
Now I have two drop down menus in my JSP for countries and provinces and I want to filter the provinces by country. I've been following this tutorial/guide to make a chain select in JavaScript.
Now I need a dynamic way to create the JavaScript array from my content. And before anyone mentions AJAX this is out of the question since our project uses portlets and we'd like to stay away from using frameworks like DWR or creating a servlet. Here is the JavaScript/JSP I have so far but it is not populating the Array with anything:
var countries = new Array();
<c:forEach items="${countryList}" var="country" varStatus="status">
countries[status.index] = new Array();
countries[status.index]['country'] = ${country.name};
countries[status.index]['provinces'] =
[
<c:forEach items="${country.provinces}" var="province" varStatus="provinceStatus">
'${province.name}'
<c:if test="${!provinceStatus.last}">
,
</c:if>
</c:forEach>
];
</c:forEach>
Does anyone know how to create an JavaScript array in JSP in the case above or what the 'best-practice' would be considered in this case? Thanks in advance!
© Stack Overflow or respective owner