如何在 Spring MVC 的 EL 中显示现有值和新创建的值?
How to display existing and newly created values in EL in Spring MVC?
只有模型的最后一个值显示为EL,所以不显示现有值。
BoardController 代码。
@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";
}
这是 JSP 代码。
<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>
首先是你第一次评论评论时的日志
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 : [{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=[]}
如上,当您创建新评论时,评论列表将从现有评论中消失。
最后一个cocList是空字符串,因为你还没有评论过
我确信EL是错误的。如何修复 EL 来解决我的问题?
即使你有
<c:forEach items="${cocList}" var="cocList">
cocList
是您的列表(模型属性)的引用,但由于您将其分配给其他东西 var="cocList"
您将产生意想不到的后果,您应该将您的列表项命名为某物别的。
<c:forEach items="${cocList}" var="item">
然后您应该能够像 ${item.blah}
一样引用该项目
想到
<c:forEach items="${cocList}" var="cocList">
作为
for (Object cocList : cocList) {
...
}
这显然是不好的,相反你想要:
<c:forEach items="${cocList}" var="item">
也就是
for (Object item : cocList) {
...
}
我相信这应该可以解决您的问题。
对于它的价值,很可能你没有使用 EL 或 SpEL,你现在正在使用 JSTL。通常 SpEL 标签看起来像 spring:...
,c:...
通常引用 JSTL。
我解决了这个问题。
我在commentList中添加了getCommentofCommentList,而不是修改查询语句。
如下
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);
}
我添加了以下代码。
commentList.get(i).put("cocList", getCommentOfCommentList)
结果,效果很好。
只有模型的最后一个值显示为EL,所以不显示现有值。
BoardController 代码。
@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";
}
这是 JSP 代码。
<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>
首先是你第一次评论评论时的日志
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 : [{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=[]}
如上,当您创建新评论时,评论列表将从现有评论中消失。
最后一个cocList是空字符串,因为你还没有评论过
我确信EL是错误的。如何修复 EL 来解决我的问题?
即使你有
<c:forEach items="${cocList}" var="cocList">
cocList
是您的列表(模型属性)的引用,但由于您将其分配给其他东西 var="cocList"
您将产生意想不到的后果,您应该将您的列表项命名为某物别的。
<c:forEach items="${cocList}" var="item">
然后您应该能够像 ${item.blah}
想到
<c:forEach items="${cocList}" var="cocList">
作为
for (Object cocList : cocList) {
...
}
这显然是不好的,相反你想要:
<c:forEach items="${cocList}" var="item">
也就是
for (Object item : cocList) {
...
}
我相信这应该可以解决您的问题。
对于它的价值,很可能你没有使用 EL 或 SpEL,你现在正在使用 JSTL。通常 SpEL 标签看起来像 spring:...
,c:...
通常引用 JSTL。
我解决了这个问题。
我在commentList中添加了getCommentofCommentList,而不是修改查询语句。
如下
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);
}
我添加了以下代码。
commentList.get(i).put("cocList", getCommentOfCommentList)
结果,效果很好。