0

我正在创建并传递给我的 jsp 的数组列表是

ArrayList countryList = new ArrayList();
countryList.add(new CountryData("1", "USA"));
countryList.add(new CountryData("2", "Canada"));
countryList.add(new CountryData("3", "Mexico"));
countryList.add(new CountryData("4", "Canada"));

在我正在使用的 jsp 页面上显示

<html:select property="country" >
<html:option value="0">Select Country</html:option>
<html:optionsCollection name="InputForm" property="countryList"
label="countryName" value="countryId" />
</html:select>

是否可以过滤jsp上的列表以仅在下拉列表中显示加拿大

4

2 回答 2

0

最简单的做法可能是将 CountryData 列表序列化为 JSON(使用无数免费的 Java JSON 库之一),并使用 JavaScript 过滤国家数组。

在 Java 中:

String countryListAsJSON = serialize(countryList);
request.setAttribute("countries", countryListAsJSON);

在 JSP 中:

<script>
    var countries = ${countries};
    //...
</script>

它将被翻译成以下 HTML 代码:

<script>
    var countries = [{"countryId": "1", "countryName": "USA"}, {"countryId": "2", "countryName": "Canada"}, ...];
    //...
</script>
于 2012-12-19T18:43:47.157 回答
0

是的,这是可能的,有几种方法可以做到:

  1. 使用小脚本:(假设所有需要的包含指令都已完成)

    <%= List<CountryData> newList = new ArrayList<CountryData>(); %>
    <html:select property="country" >
    <html:option value="0">Select Country</html:option>
    <% 
         for(CountryData cd:countryList) {
             if("Canada".equals(cd.getCountry())) {
                 newList.add(cd);
             }
         }
    %>
    <html:optionsCollection name="InputForm" property="newList" label="countryName" value="countryId" />
    </html:select>
    
  2. 使用 JSTLc:forEachc:if标签从列表中过滤掉所有不需要的元素

  3. 使用专门开发的自定义标签从您的列表中过滤除“加拿大”以外的所有国家/地区

于 2012-12-20T10:55:54.160 回答