【问题标题】:can't view records stored in DB in a web page无法在网页中查看存储在 DB 中的记录
【发布时间】:2018-07-05 12:41:31
【问题描述】:

我指的是本指南 (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 上找不到属性或字段“title”

根据它们在result.html中的排列顺序,即使与其他属性(id和内容)一起被抛出。

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

控制器:

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

有什么建议吗?

【问题讨论】:

  • System.out.println(record) 行将什么写入服务器输出?也许对 findAll()-result 的强制转换已经失败了?
  • 强制转换是正确的,因为它被打印到控制台一个引用数据库的字符串。 (字符串属于这种类型:@a2350)我不记得确切的字符串,因为我现在无法尝试运行我的应用程序。

标签: java mysql rest jpa thymeleaf


【解决方案1】:

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

【讨论】:

  • 我尝试用“result”替换“redirect: result”,但问题仍未解决。我想知道 showall 方法中的 print 是否不为 null 为什么 model.addAttribute("posts", records) 不起作用?
  • 哦,等一下。您能否将 result.html 代码更改为有效的 HTML?您正在使用自关闭标签 (
    ) 并另外关闭它们 (
    )。
  • 我试图改变它,但没有任何改变。我不认为这是 HTML 的问题,异常是指一个特定的表达式: 异常评估 SpringEL 表达式:“PostEntity.title”
  • 猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-07-19
    • 1970-01-01
    • 1970-01-01
    • 2021-04-21
    相关资源
    最近更新 更多