selectボタンでプルダウン式で表示した値を選択後、登録するのですがdbに挿入されずnullのままになってしまいます。
やり方としてはcontrollerで入れたい値を指定してviewで表記、その後は画面で選択しdb挿入の流れで考えてます。
keyかvalueどちらが入っても構いません。
controller private Map<String,String> getGenre(){ Map<String, String> selectMap = new LinkedHashMap<String, String>(); selectMap.put("1", "小説"); selectMap.put("2", "経営・戦略"); selectMap.put("3", "政治・経済"); selectMap.put("4", "IT"); selectMap.put("5", "自己啓発"); selectMap.put("6", "タレント本"); selectMap.put("7", "その他"); return selectMap; } @GetMapping("new") public String newBook(Model model) { model.addAttribute("selectBooks",getGenre()); return "books/new"; }
repository @Override public List<Book> findAll() { String sql = "SELECT book.id, title, author, publisher, buy_date, release_date, genre, over_view FROM Book"; return jdbcTemplate.query(sql,new BeanPropertyRowMapper<Book>(Book.class)); } @Override public Optional<Book> findOne(int id) { String sql = "SELECT book.id, title, author, publisher, buy_date, release_date, genre, over_view FROM book " + "WHERE id = ?"; Map<String, Object> result = jdbcTemplate.queryForMap(sql, id); Book book = new Book(); book.setId((int)result.get("id")); book.setTitle((String)result.get("title")); book.setAuthor((String)result.get("author")); book.setPublisher((String)result.get("publisher")); book.setBuyDate((Date)result.get("buy_date")); book.setReleaseDate((Date)result.get("release_date")); book.setGenre((String)result.get("Genre")); book.setOverView((String)result.get("over_view")); System.out.println(result); //bookをOptionalでラップする Optional<Book> bookOpt = Optional.ofNullable(book); return bookOpt; } @Override public void insert(Book book) { jdbcTemplate.update("INSERT INTO book(genre) VALUES(?)", book.getGenre() ); } @Override public void save(Book book) { jdbcTemplate.update("INSERT INTO book(title, author, publisher, buy_date, release_date, genre, over_view) VALUES(?, ?, ?, ?, ?, ?, ?)", book.getTitle(), book.getAuthor(), book.getPublisher(), book.getBuyDate(), book.getReleaseDate(), book.getGenre(), book.getOverView() ); } @Override public int update(Book book) { return jdbcTemplate.update("UPDATE book SET title = ?, author = ?, publisher = ?, buy_date = ?, release_date = ?, genre = ?, over_view = ? WHERE id = ?", book.getTitle(), book.getAuthor(), book.getPublisher(), book.getBuyDate(), book.getReleaseDate(), book.getGenre(),book.getOverView(), book.getId() ); } @Override public int delete(int id) { return jdbcTemplate.update("DELETE FROM book WHERE id = ?", id); } }
view <!DOCTYPE html> <html xmlns:th="http://www.thymeleaf.org"> <head> <meta charset="utf-8" /> <title>New Player - baseball</title> <link rel="stylesheet" href="/css/bootstrap.css" /> <script src="/js/jquery.js"></script> <script src="/js/bootstrap.js"></script> </head> <body> <div class="container"> <h1>New Player</h1> <!-- <body th:object="${book}"> --> <form th:action="@{/books}" th:method="post"> <div class="form-group"> <label class="control-label">タイトル</label> <input class="form-control" type="text" name="title" /> </div> <div class="form-group"> <label class="control-label">著者</label> <input class="form-control" type="text" name="author" /> </div> <div class="form-group"> <label class="control-label">出版社</label> <input class="form-control" type="text" name="publisher" /> </div> <div class="form-group"> <label class="control-label">購入日</label> <input class="form-control" type="date" name="buyDate" /> </div> <div class="form-group"> <label class="control-label">発売日</label> <input class="form-control" type="date" name="releaseDate" /> </div> <div class="form-group"> <label class="control-label">ジャンル</label> <select> <option value="">---</option> <option th:each="book : ${selectBooks}" th:value="*{book.value}" th:text="*{book.value}" th:selected="*{book.value} == *{selectBooks}"></option> </select> </div> <div class="form-group"> <label class="control-label">概要</label> <input class="form-control" type="text" name="overView" /> </div> <button class="btn btn-default" type="submit">作成</button> </form> <div class="pull-right"> <a class="btn btn-link" href="/books">一覧画面へ</a> </div> </div> </body> </html>
原因はrepositoryのdb処理がうまくいってないと考えていて調査してます。
何方かのお力を頂ければと思います。
恐縮ですが、何卒よろしくお願いいたします。
回答1件
あなたの回答
tips
プレビュー