使用JSTL、jsp、Jquery创建嵌套注释(Javaservlet)

使用JSTL、jsp、Jquery创建嵌套注释(Javaservlet),jquery,jsp,servlets,jstl,Jquery,Jsp,Servlets,Jstl,我为Comments和CommentDAO创建了JavaBean,并成功地将它们发送到jsp文件。但是,我想通过向元素添加剩余的填充来创建嵌套结构,但无法添加父注释的孙子注释 代码如下: public class Comments { private int commentID; private String comment; private int parentID; private int userID; private int articleID;

我为Comments和CommentDAO创建了JavaBean,并成功地将它们发送到jsp文件。但是,我想通过向元素添加剩余的填充来创建嵌套结构,但无法添加父注释的孙子注释

代码如下:

public class Comments {
    private int commentID;
    private String comment;
    private int parentID;
    private int userID;
    private int articleID;
    private Boolean hide;
    private int commentDepth;
    private int childCount;

    public void setCommentDepth(int commentDepth) {
        this.commentDepth = commentDepth;
    }

    public int getCommentDepth() {
        return commentDepth;
    }

    public void setChildCount(int childCount) {
        this.childCount = childCount;
    }

    public int getChildCount() {
        return childCount;
    }

    public void setHide(Boolean hide){ this.hide=hide; }

    public Boolean getHide(){ return hide; }

    public void setArticleID(int articleID) {
        this.articleID = articleID;
    }

    public int getArticleID() {
        return articleID;
    }

    public void setUserID(int userID) {
        this.userID = userID;
    }

    public int getUserID() {
        return userID;
    }

    public void setCommentID(int commentID) {
        this.commentID = commentID;
    }

    public int getCommentID() {
        return commentID;
    }

    public void setComment(String comment) {
        this.comment = comment;
    }

    public String getComment() {
        return comment;
    }

    public void setParentID(int parentID) {
        this.parentID = parentID;
    }

    public int getParentID() {
        return parentID;
    }
}
将注释更改为包含childCount和commentDepth的ThreadedComments

public static List<Comments> toThreadedComments(List<Comments> comments){

        //The resulting array of threaded comments
        List<Comments> threaded = new ArrayList<>();

        //An array used to hold processed comments which should be removed at the end of the cycle
        List<Comments> removeComments = new ArrayList<Comments>();

        //get the root comments first (comments with no parent)
        for(int i = 0; i < comments.size(); i++){
            Comments c = comments.get(i);
            if(c.getParentID() == 0){
                c.setCommentDepth(0); //A property of Comment to hold its depth
                c.setChildCount(0); //A property of Comment to hold its child count
                threaded.add(c);
                removeComments.add(c);
            }
        }

        if(removeComments.size() > 0){
            //clear processed comments
            comments.removeAll(removeComments);
            removeComments.clear();
        }

        int depth = 0;
        //get the child comments up to a max depth of 10
        while(comments.size() > 0 && depth <= 10){
            depth++;
            for(int j = 0; j< comments.size(); j++){
                Comments child = comments.get(j);
                //check root comments for match
                for(int i = 0; i < threaded.size(); i++){
                    Comments parent = threaded.get(i);
                    if(parent.getCommentID() == child.getParentID()){
                        parent.setChildCount(parent.getChildCount()+1);
                        child.setCommentDepth(depth+parent.getCommentDepth());
                        threaded.add(i+parent.getChildCount(),child);
                        removeComments.add(child);
                        continue;
                    }
                }
            }
            if(removeComments.size() > 0){
                //clear processed comments
                comments.removeAll(removeComments);
                removeComments.clear();
            }
        }

        return threaded;
    }
公共静态列表到线程注释(列表注释){
//产生的线程注释数组
List threaded=new ArrayList();
//用于保存已处理注释的数组,这些注释应在循环结束时删除
List removeComments=new ArrayList();
//首先获取根注释(无父注释)
对于(int i=0;i0){
//清除已处理的评论
注释。删除所有(删除注释);
removeComments.clear();
}
int深度=0;
//让孩子的评论最大深度为10
while(comments.size()>0&&depth 0){
//清除已处理的评论
注释。删除所有(删除注释);
removeComments.clear();
}
}
返回螺纹;
}
获取allArticleComments的函数:

 public static List<Comments> getArticleComment(int articleID) {
        List<Comments> articleComments = new ArrayList<>();
****connection to database and return all comments with the same ArticleID.

}
公共静态列表getArticleComment(int-articleID){
List articleComments=新建ArrayList();
****连接到数据库并返回具有相同ArticleID的所有注释。
}
打印嵌套注释的Jstl:(我不知道如何使用javascript使用递归函数)


${comments.commentID}:${comments.comment}

${nestedcomments.commentID}:${nestedcomments.comment}

从servlet发送JavaBean:

List<Comments> allComments = CommentDAO.getArticleComment(articleID);
                List<Comments> threadedComments = CommentDAO.toThreadedComments(allComments);

                request.setAttribute("threadedComments" , threadedComments);
List allComments=CommentDAO.getArticleComment(articleID);
List threadedComments=CommentDAO.toThreadedComments(allComments);
setAttribute(“threadedComments”,threadedComments);

前几段代码应该都很好,关于如何在jsp文件中构造嵌套注释,排在倒数第二。我认为您应该在命令类中添加list,而不是parentID,请参阅有关的详细信息。如果我添加了list,它位于mysql数据库中。我必须将注释添加到父类?
List<Comments> allComments = CommentDAO.getArticleComment(articleID);
                List<Comments> threadedComments = CommentDAO.toThreadedComments(allComments);

                request.setAttribute("threadedComments" , threadedComments);