我指的是这本指南(http://jvmhub.com/2015/08/09/spring-boot-with-thymeleaf-tutorial-part-3-spring-data-jpa/),唯一的区别是我统一了类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”
根据result.html中其他属性(id和content)的处理顺序,它甚至与其他属性(id和content)一起抛出。
如果我试图将records变量的内容打印到控制台,它会显示它不是空的,所以我不知道为什么会出现这个异常。
控制器:
@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>
有什么建议吗?
1条答案
按热度按时间sz81bmfz1#
你在做重定向的事情。执行此操作时,添加到模型中的对象不会到达进一步的过程。我记得遇到过类似的麻烦。