mardi 17 juillet 2018

How do I use the Checkbox with Java + Spring in Thymeleaf/HTML?

How do I receive either true or false from an checkbox in html with Thymeleaf over to my controller, so I can take the value either true or false and save in my DB. So far I receive these errors:

  1. org.thymeleaf.exceptions.TemplateInputException: An error happened during template parsing (template: "class path resource [templates/normal/start-dag.html]")

  2. Caused by: org.attoparser.ParseException: Error during execution of processor 'org.thymeleaf.spring5.processor.SpringInputCheckboxFieldTagProcessor' (template: "normal/start-dag" - line 24, col 44)

  3. Caused by: org.thymeleaf.exceptions.TemplateProcessingException: Error during execution of processor 'org.thymeleaf.spring5.processor.SpringInputCheckboxFieldTagProcessor' (template: "normal/start-dag" - line 24, col 44)

  4. 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

  5. java.lang.IllegalStateException: Neither BindingResult nor plain target object for bean name 'goodNightOfSleep' available as request attribute at org.springframework.web.servlet.support.BindStatus.<init>(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]

My html looks like this:

            <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>

And my Controller:

// 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";
    }

Any help appreciated :)




Aucun commentaire:

Enregistrer un commentaire