
[英]Referring to the form backing bean itself in Spring Thymeleaf?
[英]Spring Conversion in form backing bean
我实际上有两个问题。 两者都发生在相同的情况下,如下所示:
我正在使用 spring 和 thymeleaf,我想向服务器发布一个表单,该表单工作正常,但服务器无法将某些提交的数据转换为我的 bean 的属性类型。
表格:
<form th:action="@{/demo}}" th:object="${myBean}" method="post">
<label>date</label>
<input type="date" th:field="*{date}">
<label>type</label>
<select th:filed="*{type}">
<option th:each="type: ${types}" th:value="${type.id}" th:text="${type.name}"></option>
</select>
<button type="submit">Submit</button>
</form>
豆类定义:
@lombok.Data
public class MyBean{
private ZonedDateTime date;
private MyType type;
}
问题:
org.springframework.data.repository.CrudRepository
,所以我会expexted。如果你们中的任何人能帮助我,我会很高兴。
我自己解决了。
日期输入的值作为字符串发送,即“2017-12-31”,以便能够在我的表单支持 bean 中使用 ZonedDateTime 我必须注册一个自定义转换器。 这是代码:
public class ZonedDateTimeConverter implements Converter<String, ZonedDateTime> {
@Override
public ZonedDateTime convert(String source) {
return ZonedDateTime.of(LocalDate.parse(source, DateTimeFormatter.ofPattern("yyyy-MM-dd")), LocalTime.of(00, 00),
ZoneId.systemDefault());
}
}
在WebMvcConfigurer
我注册了它:
@Override
public void addFormatters(FormatterRegistry registry) {
registry.addConverter(new ZonedDateTimeConverter());
}
对于第二个问题,解决方案更简单,我唯一要做的就是用@EnableSpringDataWebSupport
注释我的应用程序,然后为我的实体豆注册一些转换器
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.