从 JSP 列表填充 JavaScript 数组 [英] Populating JavaScript Array from JSP List

查看:53
本文介绍了从 JSP 列表填充 JavaScript 数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好的,也许有人可以帮助我解决我正在尝试解决的问题.本质上,我有一个 JSP 页面,它获取 Country 对象的列表(来自 Spring Portlet SimpleFormController 的方法 referenceData(),并非完全相关,但只是提及以防万一).每个国家对象都有一个省对象集,每个省和国家都有一个名称字段:

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
}

现在我的 JSP 中有两个国家和省份的下拉菜单,我想按国家/地区过滤省份.我一直在关注这个 tutorial/guide 在 JavaScript 中进行链选择.

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.

现在我需要一种动态方式来从我的内容创建 JavaScript 数组.在任何人提到 AJAX 之前,这是不可能的,因为我们的项目使用 Portlet,我们希望避免使用 DWR 等框架或创建 servlet.这是到目前为止我拥有的 JavaScript/JSP,但它没有用任何东西填充数组:

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>

有谁知道在上述情况下如何在 JSP 中创建 JavaScript 数组,或者在这种情况下会考虑什么最佳实践"?提前致谢!

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!

推荐答案

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

now what you have is something like this in javascript

var countries = [
  {country:"USA",
  provinces: [
    "Ohio",
    "New York",
    "California"
  ]},
  {country:"Canada",
  provinces: [
    "Ontario",
    "Northern Territory",
    "Sascetchewan"
  ]},
]

另一种选择是让您的输出看起来像我发布的 javascript.

The other option would be to make your output look like the javascript I posted.

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>  
];

这篇关于从 JSP 列表填充 JavaScript 数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆