Java can';t查看网页中存储在DB中的记录

Java can';t查看网页中存储在DB中的记录,java,mysql,rest,jpa,thymeleaf,Java,Mysql,Rest,Jpa,Thymeleaf,我指的是这个指南(),唯一的区别是我统一了类PostEntity和Post,并使用名称PostEntity 当我试图通过表单将数据存储在db中时,应用程序可以工作,但它不允许我查看网页中存储的数据,如上面链接的指南所示。 网页result.html显示为 "Whitelabel Error Page This application has no explicit mapping for /error, so you are seeing this as a fallback. Thu Jul

我指的是这个指南(),唯一的区别是我统一了类PostEntity和Post,并使用名称PostEntity

当我试图通过表单将数据存储在db中时,应用程序可以工作,但它不允许我查看网页中存储的数据,如上面链接的指南所示。 网页result.html显示为

"Whitelabel Error Page
This application has no explicit mapping for /error, so you are seeing this as a fallback.

Thu Jul 05 14:25:42 CEST 2018
There was an unexpected error (type=Internal Server Error, status=500).
Exception evaluating SpringEL expression: "PostEntity.title" (template: "result" - line 11, col 9)"
控制台显示异常:

org.springframework.expression.spel.SpelEvaluationException:EL1007E:在null上找不到属性或字段“title”

它甚至与其他属性(id和内容)一起根据它们在result.html中的处理顺序抛出

如果我尝试将records变量的内容打印到控制台,它会显示它不是null,因此我无法找出出现此异常的原因

控制器:

@Controller

public class Home {
@Autowired  private PostRepository  postRepository; 

@RequestMapping(value="/", method=RequestMethod.GET)
public String index(Model model) {
    model.addAttribute("post", new PostEntity());
    return "index";
}

@RequestMapping(value = "/", method = RequestMethod.POST)
public String addNewPost( PostEntity post, BindingResult bindingResult, Model model) {
    if (bindingResult.hasErrors()) {
        return "index";
    }

    postRepository.save(post);
    List<PostEntity> records = (List<PostEntity>) postRepository.findAll();

    model.addAttribute("posts", records);
            return "redirect:result";
}

 @RequestMapping(value = "/result", method = RequestMethod.GET)
public String showAllPosts(Model model) {
     List<PostEntity> records = (List<PostEntity>) postRepository.findAll();
        for (PostEntity record : records) {
            System.out.println(record);
        }
    model.addAttribute("posts",  records);
    return "result"; 
 }
}   
索引:

    <!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<title>Spring Boot and Thymeleaf example</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
</head>
<body>
    <h3>Spring Boot and Thymeleaf, part 2 - forms</h3>
    <form action="#" th:action="@{/}" th:object="${post}" method="post">
        <table>
            <tr>
                <td>Title:</td>
                <td><input type="text" th:field="*{title}" /></td>
                <td th:if="${#fields.hasErrors('title')}" th:errors="*{title}">Title error message</td>
            </tr>
            <tr>
                <td>Content:</td>
                <td><input type="text" th:field="*{content}" /></td>
                <td th:if="${#fields.hasErrors('content')}" th:errors="*{content}">Content error message</td>
            </tr>
            <tr>
                <td><button type="submit">Submit post</button></td>
            </tr>
        </table>
    </form>
</body>
</html>

Spring Boot和Thymeleaf示例
弹簧靴和百里环,第2部分-形状
标题:
标题错误消息
内容:
内容错误消息
投递
结果

<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<title>Spring Boot and Thymeleaf example</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
</head>
<body>
    <h3>Spring Boot and Thymeleaf, part 3 - SPRING DATA JPA</h3>
    <p th:each="PostEntity : ${posts}">
        <h4>Title:</h4>
         <div th:text="${PostEntity.title}"/></div>
        <h4>ID:</h4>
         <div th:text="${PostEntity.id}"/></div>
        <h4>Content:</h4>
         <div th:text="${PostEntity.content}"/></div>
        <div>---------------------------------------------------------</div>
    </p>
</body>
</html>

Spring Boot和Thymeleaf示例
弹簧靴和Thymeleaf,第3部分-弹簧数据JPA

标题: 身份证件: 内容: ---------------------------------------------------------


有什么建议吗?

您正在执行重定向操作。执行此操作时,添加到模型中的对象不会到达进一步的过程。我记得遇到过类似的问题。

行System.out.println(记录)向服务器输出写入了什么?可能findAll()-result上的强制转换已经失败了?强制转换是正确的,因为它被打印到控制台一个引用数据库的字符串。(该字符串的类型为:@a2350)我不记得确切的字符串,因为我现在无法尝试运行我的应用程序。我尝试将“redirect:result”替换为“result”,但问题仍然没有解决。我想知道print-in-showall方法是否不是null为什么model.addAttribute(“posts”,records)不起作用?哦,等等。您能将result.html代码更改为有效的html吗?您正在使用self-closing标记()并另外关闭它们()。我试图更改它,但没有任何更改。我不认为这是HTML的问题,异常指的是一个特定的表达式:异常计算SpringEL表达式:“PostEntity.title”
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<title>Spring Boot and Thymeleaf example</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
</head>
<body>
    <h3>Spring Boot and Thymeleaf, part 3 - SPRING DATA JPA</h3>
    <p th:each="PostEntity : ${posts}">
        <h4>Title:</h4>
         <div th:text="${PostEntity.title}"/></div>
        <h4>ID:</h4>
         <div th:text="${PostEntity.id}"/></div>
        <h4>Content:</h4>
         <div th:text="${PostEntity.content}"/></div>
        <div>---------------------------------------------------------</div>
    </p>
</body>
</html>