我将分组对象列表放入ArrayList中,我希望将其作为html无序列表显示到jsp中。有关说明,请参阅附图。
到目前为止,我已经创建了一个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 :(得分: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()
}