How do I use the Checkbox with Java + Spring in Thymeleaf/HTML?
如何通过Thymeleaf将html复选框中的true或false接收到控制器,所以我可以将true或false值保存在数据库中。 到目前为止,我收到以下错误:
1 2 | org.thymeleaf.exceptions.TemplateInputException: An error happened during template parsing (template:"class path resource [templates/normal/start-dag.html]") |
1 2 | Caused by: org.attoparser.ParseException: Error during execution of processor 'org.thymeleaf.spring5.processor.SpringInputCheckboxFieldTagProcessor' (template:"normal/start-dag" - line 24, col 44) |
1 2 | Caused by: org.thymeleaf.exceptions.TemplateProcessingException: Error during execution of processor 'org.thymeleaf.spring5.processor.SpringInputCheckboxFieldTagProcessor' (template:"normal/start-dag" - line 24, col 44) |
1 2 | 2018-07-17 09:05:16.097 ERROR 6713 --- [nio-8080-exec-2] o.a.c.c.C.[.[.[/].[dispatcherServlet] : Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is org.thymeleaf.exceptions.TemplateInputException: An error happened during template parsing (template:"class path resource [templates/normal/start-dag.html]")] with root cause |
1 2 3 | java.lang.IllegalStateException: Neither BindingResult nor plain target object for bean name 'goodNightOfSleep' available as request attribute at org.springframework.web.servlet.support.BindStatus.(BindStatus.java:153) ~[spring-webmvc-5.0.7.RELEASE.jar:5.0.7.RELEASE] at org.springframework.web.servlet.support.RequestContext.getBindStatus(RequestContext.java:903) ~[spring-webmvc-5.0.7.RELEASE.jar:5.0.7.RELEASE] |
我的html看起来像这样:
1 2 3 4 5 6 7 | <table> <tr> <input type="checkbox" th:path="goodNightOfSleep"> <label th:for="${#ids.next('goodNightOfSleep')}" th:text="#{StartDay.goodNightOfSleep}">Kan du huske hvad du dr?mte?</label> <input type="checkbox" th:field="*{goodNightOfSleep}"/> </tr> </table> |
而我的控制器:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | // Start Day @GetMapping("/normal/start-dag") public String opretGoal() { return"normal/start-dag"; } @PostMapping("/normal/start-dag") public String opretGoal(@ModelAttribute StartDay startDay, BindingResult bindingResult) { if (bindingResult.hasErrors()) { return"/normal/menu"; } startDayService.createGoalOfTheDay(startDay); return"normal/menu"; } |
我的StartDay.java类:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | @Entity @Table(name ="start_day") public class StartDay { @Id @Column(name ="age_in_days", nullable = true) private int ageInDays; @Column(name ="day_created", nullable = true) private String dayCreated; @Column(name ="username", nullable = true) private String username; @Column(name ="dream_remembered", nullable = true) private boolean dreamRemembered; @Column(name ="nightmare", nullable = true) private boolean nightmare; @Column(name ="waking_time", nullable = true) private int wakingTime; @Column(name ="good_night_of_sleep", nullable = true) private boolean goodNightOfSleep; |
任何帮助表示赞赏:)
更新#1
所以我只是试图从html移动第二个th:field,所以看起来像这样:
1 2 3 4 5 6 | <table> <tr> <input type="checkbox" th:path="goodNightOfSleep"> <label th:for="${#ids.next('goodNightOfSleep')}" th:text="#{StartDay.goodNightOfSleep}">Kan du huske hvad du dr?mte?</label> </tr> </table> |
这实际上使我能够进入页面,但是我的复选框如下所示,并且不返回值:
控制器方法:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | @GetMapping("/normal/start-dag") public String opretGoal(Model model) { ... StartDay startDay = .... // e.g. new StartDay(); model.addAttribute("startDay", startDay); ... return"normal/start-dag"; } @PostMapping("/normal/start-dag") public String opretGoal(@Valid StartDay startDay, BindingResult bindingResult, Model model) { if (bindingResult.hasErrors()) { // log and/or handle errors } else { // your logic goes here startDayService.createGoalOfTheDay(startDay); } return"/normal/menu"; } |
模板代码段:
1 2 3 4 5 | <form action="... or use th:action" method="post" th:object="${startDay}"> ... <input type="checkbox" name="goodNightOfSleep"> ... </form> |
您也可以将th:field用于goodNightOfSleep-Input,但它的工作原理与上面所述类似。 Thymeleaf通过名称将字段与表单元素中定义的对象进行匹配。 如果选中此框,则该值为true,否则为false。
重点是1.)将对象添加到模型中,以及2.)接收对象作为输入参数。
警告:代码未经测试即被写入编辑器。 也许有错别字。
java.lang.IllegalStateException: Neither BindingResult nor plain target object for bean name 'goodNightOfSleep' available as request attribute
at org.springframework.web.servlet.support.BindStatus.(BindStatus.java:153) ~[spring-webmvc-5.0.7.RELEASE.jar:5.0.7.RELEASE]
at org.springframework.web.servlet.support.RequestContext.getBindStatus(RequestContext.java:903) ~[spring-webmvc-5.0.7.RELEASE.jar:5.0.7.RELEASE]
错误5是因为百里香无法找到" goodNightOfSleep"属性。
尝试在get请求的model属性中传递StartDay对象。
1 2 3 4 5 | @GetMapping("/normal/start-dag") public String opretGoal(Model model){ model.addAttribute("startDay", new startDay()); return"normal/start-dag"; } |
并确保在用户界面中定义了startDay" th:object"。
1 | <form th:action="@{normal/start-dag}" th:object="${startDay}" method="post"> |
您是否尝试返回
例如
1 2 3 4 5 6 7 | @GetMapping("/normal/start-dag") public ModelAndView opretGoal(){ ModelAndView mav = new ModelAndView("normal/start-dag"); mav.addObject("StartDay", new StartDay()); return mav; } |