【问题标题】:Form submit error, Failed to convert value of type 'java.lang.String' to required type error in browser, In spring MVC表单提交错误,无法将类型“java.lang.String”的值转换为浏览器中所需的类型错误,在 Spring MVC 中
【发布时间】: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


    【解决方案1】:

    当你使用 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/>
    

    我没有在控制器中看到您将 cmets 放入模型的位置。我想帖子里面有 cmets,所以将 cmets 的引用修改为 post.cmets

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

    【讨论】:

      【解决方案2】:

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

      解决办法是把字段重命名,比如“comment”改成“commentary”,避免在数据库中再次更改,如果有的话,只需在字段上方加上注解@Column(name="comment") .

      【讨论】:

        【解决方案3】:

        也许,来自有关 thymeleaf 模板的主模板的参考如下:

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

        但它应该看起来像:

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

        在我的场合,它帮助了我

        【讨论】:

          猜你喜欢
          • 2020-05-24
          • 1970-01-01
          • 2017-04-24
          • 2021-03-13
          • 2019-09-05
          • 2018-06-10
          • 2014-02-01
          • 2017-06-14
          • 1970-01-01
          相关资源
          最近更新 更多