前提
Springフレームワークを使用し、簡単なCRUDサイトを作成しています。
現在Create部分で詰まっています。
やりたい事
このような一覧と投稿フォームがあるページで、投稿をすると投稿詳細画面に遷移するようにしたいです。
起こっている事
投稿フォームから投稿を行った際に、以下の様にエラーが起こり詳細画面に遷移できません
Showリンクからアクセスした場合は、URLが「〜/books/1」など各idの詳細画面に遷移しますが、
投稿後に詳細画面に遷移する際には、URLが上の画像の様に「〜/books/show」となりエラーになっている状態です。
コードをどう書けば、投稿した際に、idの値を取得しページ遷移を完了させられるのかを教えていただきたいです。
該当のコード
java:BookController.java
1@Controller 2@RequestMapping("/books") 3public class BookController { 4 5 private final BookService bookService; 6 7 @Autowired 8 public BookController(BookService bookService) { 9 this.bookService = bookService; 10 } 11 12// 一覧画面 13 @GetMapping 14 public String index(BookForm bookForm, 15 BindingResult result, 16 Model model) { 17 List<Book> list = bookService.getAll(); 18 model.addAttribute("bookList", list); 19 return "books/index"; 20 } 21 22// 保存 23 @PostMapping("/complete") 24 public String complete(@Validated BookForm bookForm, 25 BindingResult result, 26 Model model, 27 RedirectAttributes redirectAttributes) { 28 if(result.hasErrors()) { 29 List<Book> list = bookService.getAll(); 30 model.addAttribute("bookList", list); 31 return "redirect:/books/index"; 32 } 33 Book book = new Book(); 34 book.setTitle(bookForm.getTitle()); 35 book.setBody(bookForm.getBody()); 36 bookService.save(book); 37// redirectAttributes.addAttribute("complete", "Book was successfully created."); 38 return "redirect:/books/show"; 39 } 40 41// 詳細画面 42 @GetMapping("/{show}") 43 public String confirm(Model model, 44 @PathVariable("show") int id, 45 @ModelAttribute("complete") String complete) { 46 Book book = bookService.findById(id); 47 model.addAttribute("bookData", book); 48 return "books/show"; 49 } 50 51}
java:BookServiceImpl.java
1@Service 2public class BookServiceImpl implements BookService { 3 4 private final BookDao dao; 5 6 @Autowired public BookServiceImpl(BookDao dao) { 7 this.dao = dao; 8 } 9 10 @Override 11 public void save(Book book) { 12 dao.insertBook(book); 13 } 14 15 @Override 16 public List<Book> getAll() { 17 return dao.getAll(); 18 } 19 20 @Override 21 public Optional<Book> getBook(int id) { 22 return dao.findById(id); 23 } 24 25 @Override 26 public Book findById(int id) { 27 return dao.findById(id).get(); 28 } 29 30}
html:index.html
1<!DOCTYPE html> 2<html xmlns:th="http://www.thymeleaf.org"> 3 <head> 4 <meta charset="UTF-8"> 5 <title>Bookers</title> 6 </head> 7 <body> 8 <h1>Books</h1> 9 <table> 10 <thead> 11 <tr> 12 <th>Title</th> 13 <th>Body</th> 14 <th></th> 15 </tr> 16 </thead> 17 <tbody> 18 <tr th:each = "book : ${bookList}"> 19 <td th:text = "${book.title}">Title</td> 20 <td th:text = "${book.body}">Body</td> 21 <td> 22 <a th:href="@{/books/{id}(id=${book.id})}">Show</a> 23 <a>Edit</a> 24 <a>Destroy</a> 25 </td> 26 </tr> 27 </tbody> 28 </table> 29 30 <h2>New book</h2> 31 <form method="post" action="#" th:action="@{/books/complete}" th:object="${bookForm}"> 32 <label for="title">Title</label><br> 33 <input id="title" name="title" type="text"><br> 34 <input type="hidden" name="title" th:value="*{title}"> 35 <div th:if="${#fields.hasErrors('title')}" th:errors="*{title}"></div> 36 <label for="body">Body</label><br> 37 <input id="body" name="body" type="text"><br> 38 <input type="hidden" name="body" th:value="*{body}"> 39 <div th:if="${#fields.hasErrors('body')}" th:errors="*{body}"></div> 40 <input type="submit" value="Create Book"> 41 </form> 42 </body> 43</html>
html:show.html
1<!DOCTYPE html> 2<html xmlns:th="http://www.thymeleaf.org"> 3 <head> 4 <meta charset="UTF-8"> 5 <title>Bookers</title> 6 </head> 7 <body> 8 <p th:text="${complete}"></p> 9 <div th:object="${bookData}"> 10 <h1 th:text="*{title}"></h1> 11 <p th:text="*{body}"></p> 12 </div> 13 <a>Edit</a> | <a th:href="@{/books}">Back</a> 14 </body> 15</html>

回答2件
あなたの回答
tips
プレビュー