下記URLよりSpringBootを勉強したてのものです。
大項目の4での住所録の作成はエラーが出て調べて作成ができましたが
課題5を行っている最中でエラーが出てきてしまい、ご教示いただきたいです。
https://www.sys-link.jp/technical/SpringBoot/SpringBoot0-0.html
新規追加のボタンを押すとエラーが出てしまいます。
コントローラは前の住所録と同じ作成方法で行っているので
htmlファイルに問題があるのかと考えておりますが、
どこに問題があるのか自分ではわからない状態です。
BbsController.java
import java.util.Date; import java.util.List; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.transaction.annotation.Transactional; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.ModelAttribute; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.servlet.ModelAndView; @Controller public class BbsController { @Autowired BbsRepository repos; /* 一覧画面(初期画面)への遷移 */ @GetMapping public ModelAndView list() { ModelAndView mav = new ModelAndView(); List<Bbs> list = repos.findAll(); mav.setViewName("users/list"); mav.addObject("data", list); return mav; } /* 新規画面への遷移 */ @GetMapping("/add") ModelAndView add() { ModelAndView mav = new ModelAndView(); Bbs data = new Bbs(); mav.addObject("formModel", data); mav.setViewName("users/new"); return mav; } /* 編集画面への遷移 */ @GetMapping("/edit") ModelAndView edit(@RequestParam int id) { ModelAndView mav = new ModelAndView(); Bbs data = repos.findById(id); mav.addObject("formModel", data); mav.setViewName("users/new"); return mav; } /* 詳細への遷移 */ @GetMapping("/show") ModelAndView show(@RequestParam int id) { ModelAndView mav = new ModelAndView(); Bbs data = repos.findById(id); mav.addObject("formModel", data); mav.setViewName("users/show"); return mav; } /* 更新処理 */ @PostMapping() @Transactional(readOnly=false) public ModelAndView save( @ModelAttribute("formModel") Bbs bbs) { bbs.setCreateDate(new Date()); repos.saveAndFlush(bbs); return new ModelAndView("redirect:users/list"); } /* 削除処理 */ @PostMapping("/delete") @Transactional(readOnly=false) public ModelAndView delete(@RequestParam int id) { repos.deleteById(id); return new ModelAndView("redirect:/"); }
BbsRepository.java
package com.example.demo; import org.springframework.data.jpa.repository.JpaRepository; public interface BbsRepository extends JpaRepository<Bbs, Long> { public Bbs findById(int id); public void deleteById(int id); }
Bbs.java
package com.example.demo; import java.util.Date; import javax.persistence.Column; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.GenerationType; import javax.persistence.Id; import javax.persistence.Table; import lombok.Getter; import lombok.Setter; @Entity @Getter @Setter @Table(name="bbs") public class Bbs { @Id @GeneratedValue(strategy = GenerationType.AUTO) private int id; @Column private Date createDate; @Column private String title; @Column private String content; @Column private String createUser; }
list.html
<h1>掲示板</h1> <table border="1"> <tr> <th class="col_create_date">作成日</th> <th class="col_title">タイトル</th> <th class="col_create_user">作成者</th> <th>詳細</th> <th>編集</th> <th>消去</th> </tr> <tr th:each="obj : ${data}"> <td th:text="${#dates.format(obj.date, 'yyyy/MM/dd')}"> <td th:text="${obj.title}"></td> <td th:text="${obj.create_user}"></td> <td> <form action="/show" method="get"> <input type="submit" value="詳細"> <input type="hidden" name="id" th:value="${obj.id}"> </form> </td> <td> <form action="/edit" method="get"> <input type="submit" value="編集"> <input type="hidden" name="id" th:value="${obj.id}"> </form> </td> <td> <form action="/delete" method="post"> <input type="submit" value="削除"> <input type="hidden" name="id" th:value="${obj.id}"> </form> </td> </tr> </table> <hr> <form action="/add"> <input type="submit" value="新規追加" /> </form> </body> </html>
new.html
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>Insert title here</title> <style> table { border-collapse: collapse; } .col_name {width:200px;} .col_data {width:400px;} form {display: inline;} </style> </head> <body> <h1>掲示板登録</h1> <form method="post" action="/" th:object="${formModel}"> <input type="hidden" name="id" th:value="*{id}"> <table border="1"> <tr> <th class="col_title">タイトル</th> <td class="col_data"> <input type="text" name="name" th:value="*{title}" size="40" maxlength= "40" /> </td> </tr> <tr> <th>内容</th> <td> <input type="text" name="content" th:value="*{content}" size="40" maxlength="40" /> </td> </tr> <tr> <th>作成者</th> <td> <input type="text" name="create_user" th:value="*{create_user}" size="40" maxlength= "40" /> </td> </tr> </table> <hr> <input type="submit" value="登 録" /> </form> <form action="/"> <input type="submit" value="戻 る" /> </form> </body> </html>
まだ回答がついていません
会員登録して回答してみよう