【发布时间】:2018-12-24 19:16:59
【问题描述】:
如何通过 Thymeleaf 将 html 中的复选框接收到 true 或 false 到我的控制器,因此我可以将值设为 true 或 false 并保存在我的数据库中。到目前为止,我收到了这些错误:
org.thymeleaf.exceptions.TemplateInputException: 模板解析时出错(模板:“类路径资源[templates/normal/start-dag.html]”)
引起:org.attoparser.ParseException: 执行处理器“org.thymeleaf.spring5.processor.SpringInputCheckboxFieldTagProcessor”时出错(模板:“normal/start-dag” - 第 24 行,第 44 列)
引起:org.thymeleaf.exceptions.TemplateProcessingException: 执行处理器“org.thymeleaf.spring5.processor.SpringInputCheckboxFieldTagProcessor”时出错(模板:“normal/start-dag” - 第 24 行,第 44 列)
2018-07-17 09:05:16.097 错误 6713 --- [nio-8080-exec-2] oaccC[.[.[/].[dispatcherServlet] : Servlet.service() 用于 servlet [dispatcherServlet] 在路径 [] 的上下文中抛出异常 [请求处理失败; 嵌套异常是 org.thymeleaf.exceptions.TemplateInputException: An error occurred during template parsing (template: "class path resource [templates/normal/start-dag.html]")] 根本原因
java.lang.IllegalStateException:Bean 名称“goodNightOfSleep”的 BindingResult 和普通目标对象都不能用作请求属性 在 org.springframework.web.servlet.support.BindStatus.(BindStatus.java:153) ~[spring-webmvc-5.0.7.RELEASE.jar:5.0.7.RELEASE] 在 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";
}
我的 StartDay.java 类:
@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,所以它看起来像这样:
<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>
【问题讨论】:
-
我认为您的 标签末尾缺少斜线。
-
@pDer666:这是有效的 html5 语法。
-
刚刚添加 - 没有任何改变,但感谢 :)
-
好的,是否应该有一个是复选框和一个否复选框?目前,第一个输入字段中的 th:path 和第二个输入字段中的 th:field 的组合没有多大意义。
-
@Mongo:我写了一个小样本。等几分钟(我在工作,我的老板对工作中的私人事情很讨厌)
标签: java spring spring-boot checkbox thymeleaf