Skip to content

Commit

Permalink
Display posts list in /posts page
Browse files Browse the repository at this point in the history
Adjust BlogController and index.html to display a list of posts when
accessing `/posts`
  • Loading branch information
lidimayra committed Jan 10, 2025
1 parent ab7c032 commit da63ee6
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 2 deletions.
7 changes: 6 additions & 1 deletion myapp/src/main/java/com/example/myapp/BlogController.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,24 @@

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PatchMapping;
import org.springframework.web.bind.annotation.DeleteMapping;

import java.util.List;

@Controller
public class BlogController {

@Autowired
private PostRepository postRepository;

@GetMapping("/posts")
public String listPosts() {
public String listPosts(Model model) {
List<Post> posts = postRepository.findAll();
model.addAttribute("posts", posts);
return "blog/index";
}

Expand Down
18 changes: 17 additions & 1 deletion myapp/src/main/resources/templates/blog/index.html
Original file line number Diff line number Diff line change
@@ -1 +1,17 @@
<p>List of Posts</p>
<!DOCTYPE html SYSTEM "http://www.thymeleaf.org/dtd/xhtml1-strict-thymeleaf-4.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org">

<h1>Blog</h1>

<dl th:each="post : ${posts}">
<dt>
<span th:text="${post.title}">Title</span>
</dt>

<dd>
<span th:text="${post.content}">Content</span>
</dd>
</dl>

<a th:href="@{/posts/new}">Submit a new post</a>

0 comments on commit da63ee6

Please sign in to comment.