首页 > 解决方案 > Thymeleaf 无法将 zoneddatetime 绑定到输入字段

问题描述

这是我的表格

<form th:action="@{/pages/org/create}" th:object="${org}" method="post" id="createOrgForm">
                    <div class="row">
                       <div class="form-group col-sm-12">
                          <label for="name">Name</label> <input class="form-control" id="name" type="text" th:field="*{name}" />
                       </div>
                    </div>

                    <div class="row">
                       <div class="form-group col-sm-12">
                          <label for="uuid">Uuid</label> <input class="form-control" id="uuid" type="text" th:field="*{uuid}" />
                       </div>
                    </div>
                    <div class="row">
                       <div class="form-group col-sm-12">
                          <label for="expiryDate">Expiry Date</label> 
                          <input class="form-control" id="expiryDate" type="date" name="expiryDate" th:value="${#temporals.format(org.expiryDate, 'dd/MMM/yyyy HH:mm', 'en')}"/>/>
                       </div>
                    </div>
                 </form>

该对象具有名称和 uuid,它们是字符串它还有一个 expiryDate,它是 zonedDateTime

我想将 expiryDate 作为 zoneddatetime 返回,但我找不到任何示例

有人知道怎么做吗?

如果需要更多详细信息,请告诉我

我有这个错误

出现意外错误(类型=错误请求,状态=400)。object='org' 的验证失败。错误计数:1

因为 org.expiryDate 是一个字符串,它需要一个 zoneddatetime

public class OrganizationDto extends TemporallyScopedEntityFlatDto {

private static final long serialVersionUID = -2191341305487098272L;

public static OrganizationDto fromEntity(final Organization o) {
    if (o == null) {
        return null;
    }
    final OrganizationDto dto = new OrganizationDto();
    DtoUtil.copyVersioned(o, dto);
    DtoUtil.copyTemporallyScoped(o, dto);
    dto.setName(o.getName());
    dto.setUuid(o.getUuid());
    return dto;
}

@ApiModelProperty(value = "The display name of this organization.")
private String name;
@ApiModelProperty(value = "The unique identifier of this organization.")
private String uuid;

private ZonedDateTime expiryDate;

public String getName() {
    return name;
}

public String getUuid() {
    return uuid;
}

public void setName(final String name) {
    this.name = name;
}

public void setUuid(final String uuid) {
    this.uuid = uuid;
}

}

标签: javathymeleaf

解决方案


推荐阅读