これは私のHTMLフォームです:日は常にnull送られ
<form ui-jp="parsley" th:action="@{/users/created}" th:object="${userCreateDto}" method="post">
<div class="row m-b">
<div class="col-sm-6">
<div class="form-group">
<label>User Valid From</label>
<input type='date' class="form-control" th:field="*{validFrom}" required/>
</div>
</div>
<div class="col-sm-6">
<div class="form-group">
<label>User Valid To</label>
<input type='date' class="form-control" th:field="*{validTill}" required/>
</div>
</div>
</div>
</form>
これはコントローラです:
@RequestMapping(value = "/created", method = RequestMethod.POST)
public String processCreateUsers(@ModelAttribute UserCreateDto userCreateDto, BindingResult error) {
log.info("Creating users with this input :"+ userCreateDto.toString());
this.userService.createUser(userCreateDto);
return "redirect:/users";
}
そして、これは
public class UserCreateDto {
private String username;
private String firstName;
private String lastName;
private String password;
@DateTimeFormat(pattern = "yyyy-mm-dd")
private Date validFrom;
@DateTimeFormat(pattern = "yyyy-mm-dd")
private Date validTill;
}
ここDTOある日は、Javaで.sql.Date
また、インポートすると私はユーザーの配列を持ってJSONファイルを介して - 私はこれを正常に実行することができます。
ビューで実行すると、validFrom
とvalidTill
の値がnullになります。
また、送信をクリックした後にフォームデータをチェックすると、必要な入力がすべて正しく設定されます。 Dateパラメータが設定されているイベント。
私はBindingResultエラーを印刷するとき、これは次のとおりです。
Failed to convert property value of type 'java.lang.String' to required type 'java.sql.Date' for property 'validFrom'; nested exception is org.springframework.core.convert.ConversionFailedException: Failed to convert from type [java.lang.String] to type [@org.springframework.format.annotation.DateTimeFormat java.sql.Date] for value '2017-12-10'; nested exception is org.springframework.core.convert.ConverterNotFoundException: No converter found capable of converting from type [org.joda.time.DateTime] to type [@org.springframework.format.annotation.DateTimeFormat java.sql.Date]
は、私がここで何をしないのですか?
注:java.util.Date works!
'BindingResult error'を出力した場合、それは何と言いますか? – holmis83
質問に追加されました。 – nirvair