当前位置: 首页 > 知识库问答 >
问题:

如何在Thymeleaf/HTML中使用Java+Spring复选框?

单于轶
2023-03-14

我如何从html中的复选框接收true或false,并将Thymeleaf传送到我的控制器,这样我就可以接收true或false的值并保存在我的数据库中。到目前为止,我收到以下错误:

  1. org.thymeleaf.exceptions.TemplateInputException:
    An error happened during template parsing (template: "class path resource [templates/normal/start-dag.html]")
    Caused by: org.attoparser.ParseException:
    Error during execution of processor 'org.thymeleaf.spring5.processor.SpringInputCheckboxFieldTagProcessor' (template: "normal/start-dag" - line 24, col 44)
    Caused by: org.thymeleaf.exceptions.TemplateProcessingException:
    Error during execution of processor 'org.thymeleaf.spring5.processor.SpringInputCheckboxFieldTagProcessor' (template: "normal/start-dag" - line 24, col 44)
    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
    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如下所示:

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

    和我的控制器:

    // 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";
    }
    
    @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:字段,所以它看起来像这样:

    <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个答案

尉迟子民
2023-03-14
@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";
}
<form action="... or use th:action" method="post" th:object="${startDay}">
    ...
    <input type="checkbox" name="goodNightOfSleep">
    ...
</form>

您还可以将th:field用于goodNightOfSleep-Input,但它的工作方式与上面的writen类似。Thymeleaf根据名称将字段与form-element中定义的对象匹配。如果选中该复选框,则该值将为true,否则将为false。

重点是1.)将对象添加到模型中,以及2.)接收对象作为输入参数。

警告:代码未经过测试就写入编辑器。也许有错别字。

 类似资料:
  • 我正在用Spring Boot、Kotlin和Thymeleaf构建一个web应用程序。我有一些HTML模板,但我想让其中一个返回XML文件。这个XML将是一个使用ThymleLab属性的ThymleLab模板。在Spring Boot中,正确的方法是什么?此外,还应下载XML。 我看过这个:Spring靴

  • 在这里,我有一个表单,其中有复选框的用户列表。我需要获得布尔值以及各个复选框的用户ID,以知道每个用户的布尔值是真的还是假的,以便在数据库中按每个用户插入值。例如: 假设我们有三个id为1,2,3的用户,那么我需要相应用户的复选框选中值,如1,0,1。因此,如果选中或没有为用户插入布尔值。 请帮助我,我不知道如何获得多个复选框的输入数据,我正在使用spring boot,spring data j

  • 提交时不调用initBinder。仅在页面加载时。因此,我的控制器无法获得userTypes对象。少了什么?谢谢!

  • 问题内容: 在Java Swing中具有每个复选框的项目列表的最佳方法是什么? 即是一个JList,其中每个项目都有一些文本和一个复选框? 问题答案: 创建一个自定义并将其分配给。 此自定义必须在方法的实现中返回a 。 但这将是不可编辑的,因为屏幕上的简单绘画取决于您何时必须“勾选”, 例如,在选中该行时将其选中(参数),但如果选择更改,则不会保留检查状态。最好在下方显示参考数据进行检查,但是您可

  • 除了使用DangerouslySetInnerHTML之外,还有其他的方法吗? 谢谢!

  • 我想在html表中显示以下一行: 这样做的正确语法是什么?