【发布时间】:2016-02-18 03:14:14
【问题描述】:
所以,我正在尝试使用 spring mvc、spring boot、spring data、jpa 和 thymeleaf 在帖子上创建 cmets,到目前为止,我可以使用控制器和路径变量访问我想要的特定页面,并且我可以按照自己的方式加载页面,但是当我提交评论时,我得到了错误
There was an unexpected error (type=Bad Request, status=400).
Failed to convert value of type 'java.lang.String' to required type 'com.example.domain.Comment'; nested exception is org.springframework.core.convert.ConversionFailedException: Failed to convert from type java.lang.String to type java.lang.Long for value 'comment 1'; nested exception is java.lang.NumberFormatException: For input string: "comment1"
此错误仅出现在我的浏览器中,在我的 IDE 的控制台中没有出现任何内容。我也可以很好地访问该页面,所以我认为我的控制器中的 get 方法没有问题,但我不确定问题出在哪里,所以我会向你们展示我的一些代码.
这是我的控制器。
private PostRepository postRepo;
@RequestMapping(value="viewCourse/post/{postId}", method=RequestMethod.GET)
public String postViewGet (@PathVariable Long postId, ModelMap model)
{
Post post = postRepo.findOne(postId);
model.put("post", post);
Comment comment = new Comment();
model.put("comment", comment);
return "post";
}
@RequestMapping(value="viewCourse/post/{postId}", method=RequestMethod.POST)
public String postViewPost (@ModelAttribute Comment comment, @PathVariable Long postId, ModelMap model)
{
Post post = postRepo.findOne(postId);
comment.setPost(post);
post.getComments().add(comment);
postRepo.save(post);
return "redirect:/viewCourse/{postId}";
}
@Autowired
public void setPostRepo(PostRepository postRepo) {
this.postRepo = postRepo;
}
这是我的 thymeleaf html 页面
<div class="PostContent">
<h2 th:text = "${post.title}"></h2>
<p th:text = "${post.content}"></p>
</div>
<br/>
<div class="CommentPost">
<form th:action="${post.id}" method="post" th:object="${comment}" id="comment">
<div class="form-group">
<textarea rows="2" th:field="${comment.comment}" class="form-control" placeholder="comment" id="comment"></textarea>
</div>
<input type="hidden" th:name="${_csrf.parameterName}" th:value="${_csrf.token}"/>
<input type="submit" value="Comment" class="btn btn-success"/>
</form>
</div>
<br/>
<div class="Comments">
<div th:each = "comment : ${comments}" th:object="${comment}">
<span th:text="${comment.comment}"></span>
</div>
<div th:if = "${#lists.isEmpty(comments)}">
There are no comments to display
</div>
</div>
</div>
同样在此页面上出现消息“没有要显示的 cmets”,就像我在代码中告诉它的那样,但即使我手动插入评论,它仍然显示“没有要显示的 cmets”数据库。
这是我的评论对象,虽然我很确定这很好。
@Entity
public class Comment {
public Long id;
public String comment;
public Post post;
public User user;
@Id
@GeneratedValue
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getComment() {
return comment;
}
public void setComment(String comment) {
this.comment = comment;
}
@ManyToOne
public Post getPost() {
return post;
}
public void setPost(Post post) {
this.post = post;
}
@ManyToOne
public User getUser() {
return user;
}
public void setUser(User user) {
this.user = user;
}
}
还有我的 postRepo,虽然这应该没问题,只是想我会包含它
public interface PostRepository extends JpaRepository <Post, Long>{
}
如果有人能看到我的问题并告诉我,那就太好了,谢谢。
【问题讨论】:
标签: spring-mvc jpa spring-boot thymeleaf