Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/spring-mvc/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/jpa/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Spring mvc 表单提交错误,无法转换类型为';java.lang.String';在spring MVC中的浏览器中出现所需类型错误_Spring Mvc_Jpa_Spring Boot_Thymeleaf - Fatal编程技术网

Spring mvc 表单提交错误,无法转换类型为';java.lang.String';在spring MVC中的浏览器中出现所需类型错误

Spring mvc 表单提交错误,无法转换类型为';java.lang.String';在spring MVC中的浏览器中出现所需类型错误,spring-mvc,jpa,spring-boot,thymeleaf,Spring Mvc,Jpa,Spring Boot,Thymeleaf,因此,我尝试使用spring mvc、spring boot、spring data、jpa和thymeleaf在帖子上创建评论,到目前为止,我可以使用控制器和pathvariables访问我想要的特定页面,并且我可以按照我想要的方式加载页面,但是当我提交评论时,我得到了错误 出现意外错误(类型=错误请求,状态=400)。 未能将“java.lang.String”类型的值转换为所需的类型“com.example.domain.Comment”;嵌套异常为org.springframework.

因此,我尝试使用spring mvc、spring boot、spring data、jpa和thymeleaf在帖子上创建评论,到目前为止,我可以使用控制器和pathvariables访问我想要的特定页面,并且我可以按照我想要的方式加载页面,但是当我提交评论时,我得到了错误

出现意外错误(类型=错误请求,状态=400)。
未能将“java.lang.String”类型的值转换为所需的类型“com.example.domain.Comment”;嵌套异常为org.springframework.core.convert.ConversionFailedException:未能将值“comment 1”的类型java.lang.String转换为类型java.lang.Long;嵌套异常是java.lang.NumberFormatException:对于输入字符串:“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页面

我的postRepo,虽然这应该是好的,但我想我会包括它

public interface PostRepository extends JpaRepository <Post, Long>{

}
公共接口PostRepository扩展了JpaRepository{
}

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

当您使用th:object时,不必引用object,您可以直接访问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>


没有要显示的注释

当您使用th:object时,不必引用object,您可以直接访问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>


没有要显示的注释

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


解决方案是将字段重命名,如“comment”改为“comment”,并避免在数据库中再次更改,如果有的话,只需在字段上方添加注释@Column(name=“comment”)。

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


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

也许,有关thymeleaf模板上主模板的引用如下所示:

  th:href="@{/post/{${post.getId()}}",
  th:href="@{/post/{postId}(postId=${post.getId()})}"
但它应该是这样的:

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

在我的场合,它对我有所帮助

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

  th:href="@{/post/{${post.getId()}}",
  th:href="@{/post/{postId}(postId=${post.getId()})}"
但它应该是这样的:

  th:href="@{/post/{${post.getId()}}",
  th:href="@{/post/{postId}(postId=${post.getId()})}"
在我的场合,它帮助了我