DropDownListの初期値をDBの値とする方法
SpringBootでHTMLにDBのテーブルデータを一覧表示しており
その中の列にDoropDownlistを置いて(データソースもDBからです)値を表示・選択しようとしているのですが
現状だとDoropDownlistの初期値がすべての行で先頭の値が指定されてる状態です。
これをDBの値と連動するにはどのようにすればよいのでしょうか?
発生している問題・エラーメッセージ
該当のソースコード
sql
1st_id Integer PRYMARYKEY 2strength Text
java
1package com.ncapl.pro; 2 3import java.util.List; 4import java.util.Map; 5 6import org.springframework.beans.factory.annotation.Autowired; 7import org.springframework.jdbc.core.JdbcTemplate; 8import org.springframework.stereotype.Controller; 9import org.springframework.ui.Model; 10import org.springframework.web.bind.annotation.RequestMapping; 11 12@Controller 13public class Test { 14 15 @Autowired 16 JdbcTemplate jdbcPostgres; 17 18 @RequestMapping("/test") 19 public String test(Model model) { 20 List<Map<String,Object>> list; 21 list = jdbcPostgres.queryForList("select * from t_strength"); 22 model.addAttribute("st_id", list); 23 model.addAttribute("strength", list); 24 return "test"; 25 } 26}
html
1<!DOCTYPE html> 2<html xmlns:th="http://www.thymeleaf.org"> 3<head> 4 <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/> 5 <title>test</title> 6 <link rel="stylesheet" type="text/css" href="master.css" /> 7</head> 8<body> 9 10 11<table id="strength" class="table table-striped table-bordered table-hover table-condensed"> 12 <tr> 13 <th>No.</th> 14 <th>列1</th> 15 </tr> 16 <tr th:each="strength : ${strength}"> 17 <td align="left" th:text="${strength.st_id}"></td> 18 <td align="left"> 19 <div> 20 <select id="st_id" name="strength"> 21 <option th:each="strength : ${st_id}" 22 th:value="${strength.st_id}" 23 th:selected="${strength.st_id == st_id}" 24 th:inline="text">[[${strength.strength}]] 25 </option> 26 </select> 27 </div> 28 </td> 29 </tr> 30</table> 31</body> 32</html>
試したこと
th:selected="${strength.st_id == st_id}"のように設定しても
テーブルの先頭の値が初期値になってしまいます。
ご指摘のとおり重複IDを下記のとおり変更したところDBの初期値を表示させることが出来ました。
html
1<!DOCTYPE html> 2<html xmlns:th="http://www.thymeleaf.org"> 3<head> 4<meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 5<title>test</title> 6<link rel="stylesheet" type="text/css" href="master.css" /> 7</head> 8<body> 9 <table id="t_strength" 10 class="table table-striped table-bordered table-hover table-condensed"> 11 <tr> 12 <th>No.</th> 13 <th>列1</th> 14 </tr> 15 <tr th:each="strength : ${strength}"> 16 <td align="left" th:text="${strength.st_id}"></td> 17 <td align="left"> 18 <div> 19 <select id="st_Id" name="Strength"> 20 <option th:each="Strength : ${st_id}" th:value="${strength.st_id}" 21 th:selected="${strength.st_id == st_Id}" th:inline="text">[[${strength.strength}]] 22 </option> 23 </select> 24 </div> 25 </td> 26 </tr> 27 </table> 28</body> 29</html>
しかし、ドロップダウンすると以下のようにその値一色(リスト内が同じ値)になってしまいます。
表示(初期値)は現在のDBの値とするけれど、変更可能なようにドロップダウン自体リスト表示は可能でしょうか?
補足情報(FW/ツールのバージョンなど)
java spring boot
IDE:eclipse 2019-06
DB:postgresql 11
回答2件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
退会済みユーザー
2019/07/30 02:22
2019/07/30 02:24
退会済みユーザー
2019/07/30 02:31
2019/07/30 02:33
2019/07/30 03:19
退会済みユーザー
2019/07/30 03:58
2019/07/30 03:59
2019/07/30 04:50