前提・実現したいこと
thymeleafのチェックボックスの値をcontrollerのPutMappingで受け取りたいです。
が、GetMappingの段階でつまずいています。
プロパティまたはフィールドの「checks」がnullで見つかりませんとエラーが発生しますが、原因かわかりません。。
発生している問題・エラーメッセージ
Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is org.thymeleaf.exceptions.TemplateProcessingException: Exception evaluating SpringEL expression: "settingEntity.checks.contains(lstWorks.id)" (template: "setting" - line 37, col 43)] with root cause org.springframework.expression.spel.SpelEvaluationException: EL1007E: Property or field 'checks' cannot be found on null at org.springframework.expression.spel.ast.PropertyOrFieldReference.readProperty(PropertyOrFieldReference.java:213) ~[spring-expression-5.3.10.jar:5.3.10] at org.springframework.expression.spel.ast.PropertyOrFieldReference.getValueInternal(PropertyOrFieldReference.java:104) ~[spring-expression-5.3.10.jar:5.3.10] at org.springframework.expression.spel.ast.PropertyOrFieldReference.getValueInternal(PropertyOrFieldReference.java:91) ~[spring-expression-5.3.10.jar:5.3.10] at org.springframework.expression.spel.ast.CompoundExpression.getValueRef(CompoundExpression.java:61) ~[spring-expression-5.3.10.jar:5.3.10] at org.springframework.expression.spel.ast.CompoundExpression.getValueInternal(CompoundExpression.java:91) ~[spring-expression-5.3.10.jar:5.3.10] at org.springframework.expression.spel.ast.SpelNodeImpl.getValue(SpelNodeImpl.java:112) ~[spring-expression-5.3.10.jar:5.3.10] at org.springframework.expression.spel.standard.SpelExpression.getValue(SpelExpression.java:337) ~[spring-expression-5.3.10.jar:5.3.10] at org.thymeleaf.spring5.expression.SPELVariableExpressionEvaluator.evaluate(SPELVariableExpressionEvaluator.java:265) ~[thymeleaf-spring5-3.0.12.RELEASE.jar:3.0.12.RELEASE] at org.thymeleaf.standard.expression.VariableExpression.executeVariableExpression(VariableExpression.java:166) ~[thymeleaf-3.0.12.RELEASE.jar:3.0.12.RELEASE] at org.thymeleaf.standard.expression.SimpleExpression.executeSimple(SimpleExpression.java:66) ~[thymeleaf-3.0.12.RELEASE.jar:3.0.12.RELEASE] at org.thymeleaf.standard.expression.Expression.execute(Expression.java:109) ~[thymeleaf-3.0.12.RELEASE.jar:3.0.12.RELEASE] at org.thymeleaf.standard.expression.Expression.execute(Expression.java:138) ~[thymeleaf-3.0.12.RELEASE.jar:3.0.12.RELEASE] at org.thymeleaf.standard.processor.AbstractStandardExpressionAttributeTagProcessor.doProcess(AbstractStandardExpressionAttributeTagProcessor.java:144) ~[thymeleaf-3.0.12.RELEASE.jar:3.0.12.RELEASE] at org.thymeleaf.processor.element.AbstractAttributeTagProcessor.doProcess(AbstractAttributeTagProcessor.java:74) ~[thymeleaf-3.0.12.RELEASE.jar:3.0.12.RELEASE]
該当のソースコード
html
1 <form th:action="@{./setting_page}" th:method="put" id="update"> 2 <div id="main" class="container"> 3 <ul class="image_list"> 4 <li th:each="lstWorks : ${lstWorksEntity}"> 5 <div class="image_box"> 6 <img th:src="'data:image/png;Base64,'+${lstWorks.stringImg}" class="thumbnail" alt="img" /> 7 <input class="disabled_checkbox" type="checkbox" name="checks" th:id="${lstWorks.id}" 8 th:value="${lstWorks.id}" th:checked="${settingEntity.checks.contains(lstWorks.id)}" /> 9 <label th:for="${lstWorks.id}" class="checkbox"></label> 10 </div> 11 </li> 12 </ul> 13 <input type="submit" form="update" id="regist" value="登録">
Java
1 @GetMapping 2 public String setting(Model model) { 3 List<WorksEntity> lstWorksEntity = worksService.worksFindAll(); 4 model.addAttribute("lstWorksEntity", lstWorksEntity); 5 model.addAttribute("checks", new SettingEntity()); 6 return "setting"; 7 } 8 9 @PutMapping 10 public String displayWorks(Model model, 11 List<WorksEntity> lstWorksEntity, SettingEntity settingEntity) 12 throws IOException { 13 14 List<String> checks = settingEntity.getChecks(); 15 worksService.worksUpdate(lstWorksEntity, checks); 16 return "redirect:/MyPortfolio/setting_page"; 17 } 18 19
Java
1@Getter @Setter 2public class SettingEntity { 3 private List<String> checks = new ArrayList<>(); 4}
試してみたこと
newしないパターンも試しましたが同じエラーでした。
Java
1 @GetMapping 2 public String setting(Model model) { 3 List<WorksEntity> lstWorksEntity = worksService.worksFindAll(); 4 model.addAttribute("lstWorksEntity", lstWorksEntity); 5 return "setting"; 6 } 7 8 @PutMapping 9 public String displayWorks(List<WorksEntity> lstWorksEntity, SettingEntity settingEntity, Model model) 10 throws IOException { 11 List<String> checks = settingEntity.getChecks(); 12 model.addAttribute("checks", checks); 13 worksService.worksUpdate(lstWorksEntity, checks); 14 return "redirect:/MyPortfolio/setting_page"; 15 }
補足情報(FW/ツールのバージョンなど)
thymeleaf
Spring boot
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2021/11/14 05:19
2021/11/14 06:33
2021/11/21 07:47
2021/11/21 07:48