2013-07-09 41 views
1

我有分組對象列表到一個ArrayList中,我想將它顯示爲一個html無序列表到一個jsp中。說明請參閱附圖。 Click here for image如何將Java中的group-by對象列表轉換爲html無序列表?

到目前爲止,我已經創建了一個Java POJO類按如下:

public class CountData { 

    private String country; 
    private String state; 
    private String city; 
    private String mfc; 
    private int count; 

    public CountData(String country, String state, String city, String mfc, int count) { 
     this.country = country; 
     this.state = state; 
     this.city = city; 
     this.mfc = mfc; 
     this.count = count; 
    } 

    public String getCity() { 
     return city; 
    } 

    public void setCity(String city) { 
     this.city = city; 
    } 
    // Other getters ans setters 
} 

還創建了一個類的對象存儲到分組列表中。這個類只是爲了測試而創建的。在實際的例子中,它由SQL語句的返回組生成。在實際的例子中,數據是不同的和更復雜的,所以想到用一些簡單的樣本數據來解釋。分組可以按任何順序。

public class CountList { 

    private List<CountData> dataList; 

    public CountList() { 
     if (this.dataList == null) { 
      this.dataList = new ArrayList<CountData>(); 
     } 
     //Sorted Grouped List 
     this.dataList.add(new CountData("China", "State3", "City3", "Female", 40)); 
     this.dataList.add(new CountData("India", "State1", "City1", "Female", 10)); 
     this.dataList.add(new CountData("India", "State1", "City1 ", "Male", 40)); 
     this.dataList.add(new CountData("India", "State2", "City2", "Female", 25)); 
     this.dataList.add(new CountData("India", "State2", "City2", "Male", 30)); 
     this.dataList.add(new CountData("USA", "IL", "Chicago", "Female", 20)); 
     this.dataList.add(new CountData("USA", "IL", "Chicago", "Male", 50)); 
     this.dataList.add(new CountData("USA ", "IL", "Schaumburg", "Female", 17)); 
     this.dataList.add(new CountData("USA", "IL", "Schaumburg", "Male", 30)); 
     this.dataList.add(new CountData("USA", "WI", "Milwaukee", "Children", 10)); 
     this.dataList.add(new CountData("USA", "WI", "Milwaukee", "Female", 25)); 
     this.dataList.add(new CountData("USA", "WI", "Milwaukee", "Male", 40)); 
     this.dataList.add(new CountData("USA", "WI", "Madison", "Female", 18)); 
     this.dataList.add(new CountData("USA", "WI", "Madison", "Male", 30)); 
    } 
    public List<CountData> getDataList() { 
     return dataList; 
    } 
    public void setDataList(List<CountData> dataList) { 
     this.dataList = dataList; 
    } 
} 

現在我想將其轉換爲HTML有序列表,像這樣:

<ul> 
    <li>China (40) 
     <ul> 
      <li>State3 (40) 
       <ul> 
        <li>City3 (40) 
         <ul> 
          <li>Female (40)</li> 
         </ul> 
        </li> 
       </ul> 
      </li> 
     </ul> 
    </li> 
    <li>India (115) 
     <ul> 
      <li>State1 (50) 
       <ul> 
        <li>City1 (50) 
         <ul> 
          <li>Female (10)</li> 
          <li>Male (40)</li> 
         </ul> 
        </li> 
       </ul> 
      </li> 
      <li>State2 (65) 
       <ul> 
        <li>City2 (65) 
         <ul> 
          <li>Female (25)</li> 
          <li>Male (30)</li> 
         </ul> 
        </li> 
       </ul> 
      </li> 
     </ul> 
    </li> 
    <li>USA (245) 
     <ul> 
      <li>IL (117) 
       <ul> 
        <li>Chicago (70) 
         <ul> 
          <li>Female (20)</li> 
          <li>Male (50)</li> 
         </ul> 
        </li> 
        <li>Schaumburg (47) 
         <ul> 
          <li>Female (17)</li> 
          <li>Male (30)</li> 
         </ul> 
        </li> 
       </ul> 
      </li> 
      <li>WI (128) 
       <ul> 
        <li>Milwaukee (75) 
         <ul> 
          <li>Children (10)</li> 
          <li>Female (25)</li> 
          <li>Male (40)</li> 
         </ul> 
        </li> 
        <li>Madison (48) 
         <ul> 
          <li>Female (18)</li> 
          <li>Male (30)</li> 
         </ul> 
        </li> 
       </ul> 
      </li> 
     </ul> 
    </li> 
</ul> 

什麼將是實現這一目標的最佳途徑。示例代碼會很有幫助。我有問題讓孩子和每個父母的數量。在實際的例子中,數據是不同的和更復雜的,所以想到用一些簡單的樣本數據來解釋。分組可以按任何順序。

謝謝。

回答

0
public String convertToHTML(ArrayList<CountData> stuffs){ 
    StringBuilder sb = new StringBuilder() 
    sb.append(「<ul>\n」); 
    for(CountData cd : stuffs){ 
     sb.append(「<li>」+cd.getCountry()+」 (「+cd.getCount()+」)\n」); 
     sb.append(「\t<ul>); 
     sb.append(「\t\t<li>」+cd.getState()+」 (「+cd.getCount()+」)\n」); 
     sb.append(「\t\t\t<ul>); 
     sb.append(「\t\t\t<li>」+cd.getCity()+」 (「+cd.getCount()+」)\n」); 
     sb.append(「\t\t\t\t<ul>); 
     sb.append(「\t\t\t\t<li>」+cd.getMfc()+」 (「+cd.getCount()+」)</li>\n」); 
     sb.append(「\t\t\t\t</ul>); 
     sb.append(「\t\t\t</li>); 
     sb.append(「\t\t</ul>); 
     sb.append(「\t</li>); 
     sb.append(「</ul>); 
     sb.append(「</li>); 
     sb.append(「\n」); 
    } 
    return sb.toString() 
} 
相關問題