How to display existing and newly created values i

2019-08-18 02:34发布

问题:

Only the last value of the model is displayed as EL, so the existing value is not displayed.

The BoardController code for that.

@RequestMapping("viewPage.do")
public String viewPage(HttpSession session,HttpServletRequest request, HttpServletResponse response,Model model, 
        @RequestParam(value="board_idx", defaultValue="0") int board_idx) throws IOException,IllegalStateException
{
    // View comment list for this post
    List<HashMap<String, Object>> commentList= bService.getBoardForComment(boardData.getBoard_idx());

    // loop (comment.size)
    for(int i = 0; i < commentList.size(); i++)
    {
        System.out.println("commentList Size : " + commentList.size());
        //Saving the board_comm_idx value in the commentList to a variable
        int board_comm_idx= (Integer) commentList.get(i).get("board_comm_idx");
        //System.out.println("board_comm_idx : " + board_comm_idx);

        // View comments in comments (viewed by board_idx, board_comm_idx)
        List<HashMap<String, Object>> getCommentOfCommentList = 
                bService.getBoardCommentOfComment(board_idx, board_comm_idx);

            model.addAttribute("cocList", getCommentOfCommentList);
            System.out.println("GetCommentOfCommentList : " + getCommentOfCommentList);
            System.out.println("cocList (Model) : " + model);
    }

    model.addAttribute("commentList", commentList); // CommentList Information
    return "viewPage";
}

Here is the JSP code.

<c:forEach items="${cocList}" var="cocList">
    <div class="commentList" style="margin-left:70px">
        <div class="what">
            <div class="nickname">
                <span>
                    ${cocList.board_c_of_c_writer}
                </span>
            </div>
            <div class="writedatezone" style="margin-left: 715px;">
                <span class="era">
                    <span class="glyphicon glyphicon-time enterReReply"></span>
                    <span class="commentWriteDate">
                        <fmt:formatDate value="${cocList.board_c_of_c_writeDate}" pattern="yyy-MM-dd"/>
                    </span>
                 </span>
             </div>
             <c:if test="${cocList.idx == idx}">
                 <div class="duzone">
                    <span class="deleteOrUpdateComment">
                        <a class="updateComment">수정</a>
                        <a class="deleteComment" onclick="deleteCoc(${cocList.board_idx},${cocList.board_c_of_c_idx})">삭제</a>
                        <input type="hidden" value="${cocList.board_comm_idx}">
                        <input type="hidden" value="${cocList.board_c_of_c}">
                        <input type="hidden" name="board_idx" value="${boardInformation.board_idx}">
                    </span>
                 </div>
                </c:if>
            </div>
            <pre>${cocList.board_c_of_c_content}</pre>
    </div>
</c:forEach>

First, it is the log of when you first commented on the comment.

GetCommentOfCommentList : [{board_idx=3, board_c_of_c_writer=웹개발자, board_c_of_c_content=Test, board_c_of_c_idx=8, board_comm_idx=5, idx=4, board_c_of_c_writeDate=2017-11-30 10:33:06.0}]
cocList (Model) : {cocList=[{board_idx=3, board_c_of_c_writer=웹개발자, board_c_of_c_content=Test, board_c_of_c_idx=8, board_comm_idx=5, idx=4, board_c_of_c_writeDate=2017-11-30 10:33:06.0}]}

After that, if you make a new comment,

GetCommentOfCommentList : [{board_idx=3, board_c_of_c_writer=웹개발자, board_c_of_c_content=Test, board_c_of_c_idx=8, board_comm_idx=5, idx=4, board_c_of_c_writeDate=2017-11-30 10:33:06.0}]
cocList (Model) : {cocList=[{board_idx=3, board_c_of_c_writer=웹개발자, board_c_of_c_content=Test, board_c_of_c_idx=8, board_comm_idx=5, idx=4, board_c_of_c_writeDate=2017-11-30 10:33:06.0}]}
GetCommentOfCommentList : []
cocList (Model) : {cocList=[]}

As above, when you create a new comment, the comment list will disappear from the existing comment.

The last cocList is an empty string because you have not commented on the comment.

I am convinced that EL is wrong. How do I fix the EL to solve my problem?

回答1:

Even though you have the

<c:forEach items="${cocList}" var="cocList">

cocList is a reference your list (model attribute), but since you are assigning it to something else var="cocList" you are going to have unintended consequences, you should name your list item to something else.

<c:forEach items="${cocList}" var="item">

You should then be able to reference the item like ${item.blah}

Think of

<c:forEach items="${cocList}" var="cocList">

as

for (Object cocList : cocList) {
   ...
}

Which obviously is bad, instead you want:

<c:forEach items="${cocList}" var="item">

which is

for (Object item : cocList) {
   ...
}

I believe that should fix your problem.

For what it is worth as well, most likely, you are not using EL or SpEL, you are using JSTL at the moment. Normally the SpEL tags are going to look like spring:..., c:... normally references JSTL.



回答2:

I solved this problem.

I added getCommentofCommentList to commentList instead of modifying the query statement.

As follows.

    for(int i = 0; i < commentList.size(); i++)
    {
        System.out.println("commentList Size : " + commentList.size());
        // commentList 에 담긴 board_comm_idx  값을 변수에 저장
        int board_comm_idx= (Integer) commentList.get(i).get("board_comm_idx");
        //System.out.println("board_comm_idx : " + board_comm_idx);

        // 댓글에 댓글 조회 (board_idx , board_comm_idx으로 조회)
        List<HashMap<String, Object>> getCommentOfCommentList = 
                bService.getBoardCommentOfComment(board_idx, board_comm_idx);

        commentList.get(i).put("cocList", getCommentOfCommentList);
    }

I added the following code.

commentList.get(i).put("cocList", getCommentOfCommentList)

as the result, It's very well work.