2012-12-19 37 views
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上只顯示加拿大的下拉菜單

+2

你笑過濾掉除「加拿大」的所有國家uld在你的java代碼而不是在JSP中進行過濾。 – admenva

+0

過濾取決於我認爲,沒有任何差別在另一個費爾德 – user1516790

+1

由用戶輸入的值。無論如何,只要用戶在其他字段中選擇了一個值,就應該使用javascript進行過濾。 – admenva

回答

0

最簡單的做法是將CountryData的列表序列化爲JSON(使用無數Java JSON庫中的一種),並使用JavaScript過濾ar一線國家。

在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> 
0

是的,這是可能的,有幾個如何做到這一點:

  1. 使用的scriptlet: (假設所有需要的包含指令完成)使用JSTL要麼c:forEachc:if標籤使用自定義標籤特別濾除從列表

  2. 所有不需要的元素

    <%= 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> 
    
  3. 發展到從你的列表

相關問題