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' –