从 JSP 列表填充 JavaScript 数组

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3040645/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 03:03:59  来源:igfitidea点击:

Populating JavaScript Array from JSP List

javascriptarraysjspjstl

提问by Ian Dallas

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:

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

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/guideto make a chain select in JavaScript.

现在我的 JSP 中有两个国家和省份的下拉菜单,我想按国家/地区过滤省份。我一直在按照本教程/指南在 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:

现在我需要一种动态方式来从我的内容创建 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>

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!

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

回答by John Hartsock

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> 

now what you have is something like this in javascript

现在你在javascript中拥有这样的东西

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

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

另一种选择是使您的输出看起来像我发布的 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>  
];

回答by Ben S

Have you considered using JSON? There are several libraries out there that can take a generic collection and output JSON for Java and other languages.

你考虑过使用JSON吗?有几个库可以为 Java 和其他语言采用通用集合并输出JSON

回答by Pointy

The primary problem in your code is that you forgot to put "status.index" inside ${ }.

您代码中的主要问题是您忘记将“status.index”放入${ }.

countries[${status.index}] = new Array();

Now, that said, that would be a pretty bad way to do things because you'd end up with a lotof Javascript code in your page. Much much better to create the list using Javascript object notation as in the second of @John Hartsock's answers.

现在,这就是说,这将是一种非常糟糕的做事方式,因为您最终会在您的页面中使用大量Javascript 代码。使用 Javascript 对象表示法创建列表要好得多,就像@John Hartsock 的第二个答案一样。