好吧,也许有人可以帮我解决一个问题.本质上,我有一个JSP页面,它获取一个Country对象的列表(来自 Spring Portlet SimpleFormController的方法referenceData(),并不完全相关,只是提及它).每个Country对象
public class Country { private String name; private Set<Province> provinces; //Getters and setters } public class Province { private String name; //Getters and setters }
现在我在国家和省份的JSP中有两个下拉菜单,我想按国家/地区过滤省份.我一直在追踪这个tutorial/guide,用JavaScript做链式选择.
现在我需要一个动态的方式从我的内容创建JavaScript数组.在任何人提到AJAX之前,这是我们的问题,因为我们的项目使用portlet,我们希望远离使用框架,如DWR或创建一个servlet.这是迄今为止的JavaScript / JSP,但它并没有填充任何数组:
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>
有没有人知道如何在上述情况下在JSP中创建一个JavaScript数组,或者在这种情况下会考虑什么“最佳实践”?提前致谢!
var countries = new Array(); <c:forEach items="${countryList}" var="country" varStatus="status"> countryDetails = new Object(); countryDetails.country = ${country.name}; var provinces = new Array(); <c:forEach items="${country.provinces}" var="province" varStatus="provinceStatus"> provinces.push(${province.name}); </c:forEach> countryDetails.provinces = provinces; countries.push(countryDetails); </c:forEach>
现在你在javascript中有这样的东西
var countries = [ {country:"USA", provinces: [ "Ohio", "New York", "California" ]}, {country:"Canada", provinces: [ "Ontario", "Northern Territory", "Sascetchewan" ]}, ]
另一个选择是使您的输出看起来像我发布的javascript.
var countries = [ <c:forEach items="${countryList}" var="country" varStatus="status"> {country: '${country.name}', provinces : [ <c:forEach items="${country.provinces}" var="province" varStatus="provinceStatus"> '${province.name}' <c:if test="${!provinceStatus.last}"> , </c:if> </c:forEach> ]} <c:if test="${!status.last}"> , </c:if> </c:forEach> ];