1

我将分组对象列表放入 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 语句返回的 group 生成的。在实际的例子中,数据是不同的,更复杂,所以想用一些简单的样本数据来解释。分组可以按任何顺序。

    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>

实现这一目标的最佳方法是什么。示例代码会有所帮助。我在获取孩子和计算每个父母时遇到问题。在实际的例子中,数据是不同的,更复杂,所以想用一些简单的样本数据来解释。分组可以按任何顺序。

谢谢你。

4

1 回答 1

1
public String convertToHTML(ArrayList<CountData> stuffs){
    StringBuilder sb = new StringBuilder()  
    sb.append(“&lt;ul>\n”);
    for(CountData cd : stuffs){
        sb.append(“&lt;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(“&lt;/ul>);
        sb.append(“&lt;/li>);
        sb.append(“\n”);
    }
    return sb.toString()
}
于 2015-05-04T19:41:54.513 回答