使用嵌套迭代器使用两个不同的arraylists的困难

时间:2013-11-20 12:20:44

标签: arraylist struts2 iterator nested

晚安。

这很糟糕......我是Struts2的新手。我正在开发使用以下2个数组列表的论坛应用程序: 1)public ArrayList forumData = new ArrayList<>(); 2)public ArrayList replyList = new ArrayList<>(); 在同一个类中的arraylist上面是forumAction。

现在,在forumData列表中,我将所有帖子数据与postid以及类似方式存储在replyList中,我用postid存储了所有回复。 现在我想像嵌套循环一样迭代。在forumData的postid匹配回复列表的帖子ID匹配然后显示回复。

我希望显示如下:

POST1:
    REPLY1
    REPLY2
POST2
    REPLY1
    REPLY2

以下是我的代码:

forumAction.jsp

//for post data
private String message;
private String post_title;
private String post_detail;
private String ctime;
private String fname;
private String lname;
private String orgname;
private String country;
private String uimage;
private int post_id;


//for reply data
private int reply_id;
private String reply_desc;
private String rtime;
private String rfname;
private String rlname;
private String rorgname;
private String rcountry;
private String ruimage;
private int rpostid;
    public ArrayList<forumAction> lst = new ArrayList<>();
     public ArrayList<forumAction> replyList=new ArrayList<>();
//getter and setter done <br/>
// i called this function when my viewForum link will be clicked

public String viewForum() {
    try {

        this.openConncection();


        Statement stmt = con.createStatement();
        ResultSet rs = stmt.executeQuery("select * from forum_post");

        while (rs.next()) {
            forumAction f=new forumAction();

            Statement ntst1=con.createStatement();
            ResultSet rsnt1=ntst1.executeQuery("select firstname,lastname,orgname,country,user_image from user where userid="+ rs.getString(2));
            while(rsnt1.next()){
                f.setFname(rsnt1.getString(1));
                f.setLname(rsnt1.getString(2));
                f.setOrgname(rsnt1.getString(3));
                f.setCountry(rsnt1.getString(4));
                f.setUimage(rsnt1.getString(5));
            }

            f.setPost_id(rs.getInt(1));
            f.setPost_title(rs.getString(3));
            f.setPost_detail(rs.getString(4));
            f.setCtime(rs.getString(5));
            lst.add(f);

        }

        //Get all reply data
        Statement ntst3=con.createStatement();
        ResultSet rsnt3=ntst3.executeQuery("select * from forum_reply");
        while(rsnt3.next()){
            System.out.println(rsnt3.getString(4));
            forumAction reply=new forumAction();

            Statement ntst4=con.createStatement();
            ResultSet rsnt4=ntst4.executeQuery("select firstname,lastname,orgname,country,user_image from user where userid="+ rsnt3.getString(2));
            while(rsnt4.next()){
                reply.setRfname(rsnt4.getString(1));
                reply.setRlname(rsnt4.getString(2));
                reply.setRorgname(rsnt4.getString(3));
                reply.setRcountry(rsnt4.getString(4));
                reply.setRuimage(rsnt4.getString(5));
            }

            reply.setReply_id(rsnt3.getInt(1));
            reply.setRpostid(rsnt3.getInt(3));
            reply.setReply_desc(rsnt3.getString(4));
            reply.setRtime(rsnt3.getString(5));

            replyList.add(reply);
        }


    } catch (Exception e) {
        e.printStackTrace();
    }
return SUCCESS;
}

viewforum.jsp

<div id="forum>
        //first iterator for post data
       <s:iterator value="lst">
             <s:property value="post_title" />
             <s:property value="post_detail" />
                    //second iterator for reply data
                     <s:iterator value="replyList">
                           //if first iterator postid and this reply postid is same
                           <s:if test="post_id == rpostid">
                                      <s:property value="reply_desc" />
                            </s:if>
                     </s:iterator>
       </s:iterator>
</div>

.... 我尝试了很多。但我找不到正确的方法。 请有人帮帮我。

1 个答案:

答案 0 :(得分:0)

试试这个,

 <s:if test="lst.size() >0">
        <s:if test="replyList.size() > 0">
            <s:iterator value="lst" status="lst_status">
                <s:property value="lst.get(#lst_status).getPostTitle()" />
                <s:property value="lst.get(#lst_status).getPostDetail()" />
                <s:iterator value="replyList" status="replyList_status">
                    <s:if test="lst.get(#lst_status).getPostId() == replyList.get(#replyList_status).getRpostid()">
                        <s:property value="reply_desc" />
                    </s:if>
                </s:iterator>
            </s:iterator>
        </s:if>
    </s:if>