0
我在評論/回覆系統上測試,並且遇到了問題。 我有評論和回覆模式。Spring MVC休眠添加兩個ModelAttribute使用<form:hidden>
@Entity
public class Reply {
@Id
@GeneratedValue
private int replyId;
private String body;
private String replyOwner;
private Date datePosted;
@ManyToOne
@JoinColumn(name="commentedOn")
private Comment commentedOn;
//getters and setters
@Entity
public class Comment {
@Id
@GeneratedValue
private int comment_id;
private String commentBody;
private String commentOwner;
private Date datePosted;
@OneToMany
private List<Reply> replies;
//getters and setters
這是兩種形式我想使用:
<c:forEach items="${commentList}" var="comment">
<div class="comment-box">
<div class="comment-head">
<h6 class="comment-name by-author"><a
href="http://creaticode.com/blog">${comment.commentOwner}</a></h6>
<span>${comment.datePosted}</span>
<i class="fa fa-reply"></i>
<i class="fa fa-heart"></i>
</div>
<div class="comment-content">
${comment.commentBody}
<br>
<button id="replyButton">Reply</button>
</div>
<ul class="comments-list reply-list">
<li>
<!--////////////////////////////////////////////////////////////////////////-->
<div id="replym8">
<form:form action="${pageContext.request.contextPath}/commentTest" method="post" commandName="reply">
<div class="comment-box">
<div class="comment-content">
<label for="reply">Reply</label>
<form:textarea path="body" id="reply" class="form-Control" accept-charset="UTF-8"/>
</div>
<c:set var="commentID" value="${comment.comment_id}"/>
<input type="submit" value="Post reply" class="btn btn-default">
</div>
</form:form>
</div>
<!--////////////////////////////////////////////////////////////////////////-->
<c:forEach items="${replyList}" var="onereply">
<c:if test="${onereply.commentedOn.comment_id == onecomment.comment_id}">
<div class="comment-box">
<div class="comment-head">
<h6 class="comment-name"><a
href="http://creaticode.com/blog">${onereply.replyOwner}</a></h6>
<span>${onereply.datePosted}</span>
<i class="fa fa-reply"></i>
<i class="fa fa-heart"></i>
</div>
<div class="comment-content">
${onereply.body}
</div>
</div>
</c:if>
</c:forEach>
</li>
</ul>
</c:forEach>
</div>
</div>
</ul>
</div>
<form:form action="${pageContext.request.contextPath}/commentTest" method="post" commandName="comment">
<div class="comment-box">
<div class="comment-head">
<label for="comment">Comment_Owner</label>
<form:textarea path="commentOwner" id="commentOwner" class="form-Control" accept-charset="UTF-8"/>
</div>
<div class="comment-content">
<label for="comment">Comment</label>
<form:textarea path="commentBody" id="comment" class="form-Control" accept-charset="UTF-8"/>
</div>
</div>
<input type="submit" value="Dodaj" class="btn btn-default">
</form:form>
的問題是在<form:hidden path="commentedOn" value="${commentID}"/>
,我已經使用<form:hidden path="commentedOn" value="$comment.comment_id"/>
這兩種剛返回錯誤400還試圖
這是控制器:
@RequestMapping("/commentTest")
public String commentTest(Model model) {
Comment comment = new Comment();
Reply reply = new Reply();
model.addAttribute("comment", comment);
model.addAttribute("reply", reply);
List<Comment> commentList = commentService.getComments();
List<Reply> replyList = commentService.getRepliesByComment();
model.addAttribute("replyList", replyList);
model.addAttribute("commentList", commentList);
return "commentTest";
}
@RequestMapping(value = "/commentTest", method = RequestMethod.POST)
public String commentTestPost(@ModelAttribute("comment") Comment comment, Model model, @ModelAttribute("reply") Reply reply, BindingResult bindingResult) {
if (bindingResult.hasErrors()) {
return "commentTest";
}
commentService.addComment(comment);
commentService.addReply(reply);
List<Comment> commentList = commentService.getComments();
List<Reply> replyList = commentService.getRepliesByComment();
model.addAttribute("replyList", replyList);
model.addAttribute("commentList", commentList);
return "commentTest";
}
.addComment
和.addReply
方法只使用.saveOrUpdate
使用Hibernate和那兩個工作。
感謝您的幫助提前。 :)
你不必''在表單中 –
嘗試'<形式:隱藏路徑= 「commentedOn.comment_id」 值=「$ {評論。 comment_id}「/>'回覆'commentedOn' –