Spring Boot + thymeleaf でチェックボックスを表示する

実現したいこと

Spring Boot + thymeleafでチェックボックスを表示したいです。
th:checkedを使用すると、エラーなく表示されるのですが、
Controller側でチェックされているかどうかを取得したいので
th:fieldを使用したいのですが、エラーになって表示されません。
よろしくお願いします。

発生している問題・エラーメッセージ

2023-02-28 14:31:55.480 ERROR 18200 --- [nio-8081-exec-3] o.s.b.w.servlet.support.ErrorPageFilter : Forwarding to error page from request [/search] due to exception [Error during execution of processor 'org.thymeleaf.spring5.processor.SpringInputCheckboxFieldTagProcessor' (template: "tablePermitList" - line 112, col 49)] org.thymeleaf.exceptions.TemplateProcessingException: Error during execution of processor 'org.thymeleaf.spring5.processor.SpringInputCheckboxFieldTagProcessor' (template: "tablePermitList" - line 112, col 49) at org.thymeleaf.processor.element.AbstractAttributeTagProcessor.doProcess(AbstractAttributeTagProcessor.java:117) ~[thymeleaf-3.0.12.RELEASE.jar:3.0.12.RELEASE] at org.thymeleaf.processor.element.AbstractElementTagProcessor.process(AbstractElementTagProcessor.java:95) ~[thymeleaf-3.0.12.RELEASE.jar:3.0.12.RELEASE] at org.thymeleaf.util.ProcessorConfigurationUtils$ElementTagProcessorWrapper.process(ProcessorConfigurationUtils.java:633) ~[thymeleaf-3.0.12.RELEASE.jar:3.0.12.RELEASE] at org.thymeleaf.engine.ProcessorTemplateHandler.handleStandaloneElement(ProcessorTemplateHandler.java:918) ~[thymeleaf-3.0.12.RELEASE.jar:3.0.12.RELEASE] at org.thymeleaf.engine.StandaloneElementTag.beHandled(StandaloneElementTag.java:228) ~[thymeleaf-3.0.12.RELEASE.jar:3.0.12.RELEASE]

該当のソースコード

HTML

1<!DOCTYPE html> 2<html xmlns:th="http://www.thymeleaf.org"> 3<head> 4 <meta charset="UTF-8"> 5 <link th:href="@{/css/style.css}" rel="stylesheet"> 6 <script th:src="@{/js/common.js}"></script> 7 <title>EUCtools</title> 8<title>テーブル参照可否管理</title> 9</head> 10<body> 11<form th:action="@{/tablePermitList}" th:object="${tablePermitDto}" th:method="post"> 12<div> 13 <table cellpadding="0" cellspacing="0" style="width:450px"> 14 <col style="width:150px"/> 15 <col style="width:150px"/> 16 <col style="width:150px"/> 17 <tr style="height:35px"> 18 <td class="center"> 19 <select th:field="*{groupid}" style="width:130px"> 20 <option value=""></option> 21 <option th:each="grouplist : ${grouplist}" th:value="${grouplist.groupid}" th:text="${grouplist.group_name}"></option> 22 </select> 23 </td> 24 <td class="center"> 25 <select th:field="*{permit}" style="width:130px"> 26 <option value=""></option> 27 <option value="A">管理者</option> 28 <option value="B">利用ユーザ</option> 29 </select> 30 </td> 31 <td class="center"> 32 <select th:field="*{table_kbn}" style="width:130px"> 33 <option value=""></option> 34 <option value="M">マスター</option> 35 <option value="N">名称マスター</option> 36 <option value="T">トランザクション</option> 37 <option value="K">加工DB</option> 38 </select> 39 </td> 40 </tr> 41 </table> 42 </div> 43 </td> 44 </tr> 45 </table> 46 <div style="height:362px"> 47 <table> 48 <tr> 49 <td> 50 <input type="image" th:formaction="@{/search}" id ="submit" name="submit" src="images/btn_kensaku.gif" alt="検索" class="imgBtn"/> 51 </td> 52 </tr> 53 </table> 54 </td> 55 <td> 56 <table width="480px" cellpadding="0" cellspacing="0"> 57 <tr> 58 <td> 59 <div style="width:460px"> 60 <table cellpadding="0" cellspacing="0" style="width:440px"> 61 <col style="width:30px"/> 62 <col style="width:130px"/> 63 <col style="width:280px"/> 64 <tr> 65 <th class="list"></th> 66 <th class="list">テーブルID</th> 67 <th class="list">テーブル名</th> 68 </tr> 69 </table> 70 </div> 71 </td> 72 </tr> 73 <tr> 74 <td> 75 <div style="height:398px; overflow-y:scroll;width:460px"> 76 <table cellpadding="0" cellspacing="0" style="width:440px"> 77 <col style="width:30px"/> 78 <col style="width:130px"/> 79 <col style="width:280px"/> 80 <tr th:each="tablePermit : ${tablePermitList}" th:object="${tablePermitList}"> 81 <td class="left"><input type="checkbox" th:field="*{possible}" /></td> 82 <td class="left"><span th:text="*{tableid}" /></td> 83 <td class="left"><span th:text="*{table_name}" /></td> 84------間は抜粋------- 85 86</form> 87</body> 88</html>

Java(@Controller)

12@Controller3public class TablePermitController {4 @Autowired5 HttpSession session;6 @Autowired7 TablePermitService tablePermitService;8 @Autowired9 TablePermitRepository tablePermitRepository;10 @Autowired11 GroupService groupService;12 13 @PostMapping(value = "/search")14 @RequestMapping(value = "/search", method = RequestMethod.POST)15 public String search(@Validated @ModelAttribute TablePermitDto tablePermitDto, Model model) {16 List<TablePermit> tablePermitList = tablePermitService.findPermit(tablePermitDto.getGroupid(), tablePermitDto.getPermit(), tablePermitDto.getTable_kbn());17 model.addAttribute("tablePermitList", tablePermitList);18 List<UsysGroup> groupList = groupService.findAllByOrderByGroupid();19 model.addAttribute("grouplist", groupList);20 return "tablePermitList";21 }22}23

Java(@Service)

1@Service2public class TablePermitService {3 4 @Autowired5 TablePermitRepository tablePermitRepository;6 7 public List<TablePermit> findPermit(String groupid, String permit, String table_kbn) {8 return tablePermitRepository.findPermit(groupid, permit, table_kbn);9 }10}11

Java(@Repository)

1@Repository2public interface TablePermitRepository extends JpaRepository<TablePermit, String> {3 4 @Query(value = "SELECT possible, p.tableid AS tableid, table_name FROM usys_permit p JOIN usys_table t ON p.tableid = t.tableid " +5 "WHERE p.groupid = :groupid AND p.permit = :permit AND t.table_kbn = :table_kbn ORDER BY t.no", nativeQuery = true)6 List<TablePermit> findPermit(@Param("groupid") String groupid, @Param("permit") String permit, @Param("table_kbn") String table_kbn);7 8}

Java(@Entity)

1@Entity2@Data3public class TablePermit implements Serializable{4 5 public boolean possible;6 7 @Id8 public String tableid;9 10 public String table_name;11 12}13

コメントを投稿

0 コメント