表单提交错误,无法在浏览器中将 'java.lang.String' 类型的值转换为所需类型错误,在春季 MVC 中



所以,我试图使用springmvc、spring-boot、spring-data、jpa和thymelaf在帖子上创建评论,到目前为止,我可以使用控制器和路径变量访问我想要的特定页面,我可以随心所欲地加载页面,但当我提交评论时,我会收到错误

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;
}

这是我的thymelaf 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>

同样在这个页面上,消息会出现"没有要显示的评论",就像我在代码中告诉它的那样,但即使我手动将评论插入数据库,它仍然会显示"没有要展示的评论"。

这是我的评论对象,尽管我很确定这很好。

@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>{
}

如果有人能看到我的问题,让我知道,那就太棒了,谢谢。

当您使用th:object时,不必引用对象,您可以直接访问对象的属性。尝试使用此代码:

<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}" 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 th:each = "comment : ${post.comments}" th:object="${comment}">
    <span th:text="*{comment}"></span>
</div>
<div th:if = "${#lists.isEmpty(post.comments)}">
    There are no comments to display
</div>
</div>
</div>

问题是class-Comment和字段-Comment-的名称相同,关于不敏感的方式,由于Java反射用于读取字段及其类而导致问题。

解决方案是将字段重命名,如将"comment"重命名为"comments",并避免在数据库中再次更改,如果有更改,只需将注释@Column(name="comment")放在字段上方即可。

也许,来自thymelaf模板上主模板的引用看起来像:

  th:href="@{/post/{${post.getId()}}",

但它应该看起来像:

  th:href="@{/post/{postId}(postId=${post.getId()})}"

在我的场合,它帮助了我

相关内容

最新更新