Skip to content

Commit 0aa88c3

Browse files
committed
Enable show for posts
1 parent b65da3a commit 0aa88c3

File tree

3 files changed

+20
-3
lines changed

3 files changed

+20
-3
lines changed

myapp/src/main/java/com/example/myapp/BlogController.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,12 @@ public String createPost(Post post) {
3232
}
3333

3434
@GetMapping("/posts/{postId}")
35-
public String showPost() {
35+
public String showPost(@PathVariable("postId") long id, Model model) {
36+
Post post = postRepository.findById(id)
37+
.orElseThrow(() -> new IllegalArgumentException("Invalid Post Id:" + id));
38+
39+
model.addAttribute("post", post);
40+
3641
return "blog/show";
3742
}
3843

myapp/src/main/resources/templates/blog/index.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ <h1>Blog</h1>
99
</dt>
1010

1111
<dd>
12-
<span th:text="${post.content}">Content</span>
12+
<a th:href="@{/posts/{id}(id=${post.id})}">Show</a>
1313
<a th:href="@{/posts/{id}/edit(id=${post.id})}">Edit</a>
1414
</dd>
1515
</dl>
Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,13 @@
1-
<p>Post title</p>
1+
<!DOCTYPE html SYSTEM "http://www.thymeleaf.org/dtd/xhtml1-strict-thymeleaf-4.dtd">
2+
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org">
3+
4+
<body>
5+
<h1 th:text="${post.title}"></h1>
6+
<hr>
7+
<p th:text="${post.content}"></p>
8+
9+
<p><a th:href="@{/posts/{id}/edit(id=${post.id})}">Edit</a></p>
10+
<hr>
11+
<a th:href="@{/posts}">Go back to posts</a>
12+
</body>
13+
</html>

0 commit comments

Comments
 (0)