java - 无法在网页中查看数据库中存储的记录

标签 java mysql rest jpa thymeleaf

我指的是本指南( http://jvmhub.com/2015/08/09/spring-boot-with-thymeleaf-tutorial-part-3-spring-data-jpa/ ),唯一的区别是我统一了 PostEntity 和 Post 类,并假设名称为 PostEntity。

当我尝试通过表单将数据存储在数据库中时,应用程序可以工作,但它不允许我查看网页中存储的数据,如上面链接的指南所示。 网页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 上找不到属性或字段“标题”

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

如果我尝试将记录变量的内容打印到控制台,它会显示它不为空,所以我无法弄清楚为什么会出现此异常。

Controller :

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

模型类:

@Entity
public class PostEntity {

public PostEntity() {}

public PostEntity(String title, String content) {
    this.title = title;
    this.content = content;
}

@Id
@GeneratedValue(strategy = GenerationType.AUTO)
public int id;

public String title;

public String content;

public int getId() {
    return id;
}
public void setId(int id) {
    this.id = id;
}
public String getTitle() {
    return title;
}
public void setTitle(String title) {
    this.title = title;
}
public String getContent() {
    return content;
}
public void setContent(String content) {
    this.content = content;
}
}

索引:

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

结果

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

有什么建议吗?

最佳答案

你正在做这个重定向的事情。执行此操作时,添加到模型中的对象不会进入进一步的过程。我记得遇到过类似的麻烦。

关于java - 无法在网页中查看数据库中存储的记录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51191586/

相关文章:

java - Mockito - 如何监视 doAnswer 中的调用参数

mysql - 查询需要很长时间才能执行

java - 测试 Spring Boot REST API

java - 使用 EJML 的协方差

java - Spring 启动 : how to return error status code in prehandle of HandlerInterceptor

JavaFX FXML - 带有图标和文本的按钮

php - WP迁移,无法登录WP admin

mysql - Laravel 5 - 用组合结果 Eloquent 地加入 3 个表

rest - 如何集成 Ambari REST API 用于集群监控示例

rest - 在 node.js 中自动生成 Rest 服务