[英]Time field in Play Framework
我正在嘗試在Java Play Framework應用程序中創建簡單的時間字段。 我已經准備了一堂課:
public class Schedule extends AbstractEntity {
@Temporal(TemporalType.TIME)
public Date arrival;
@Temporal(TemporalType.TIME)
public Date departure;
@Enumerated(EnumType.STRING)
public Variant variant;
}
我嘗試通過此模板獲取一些出發和到達時間:
<div class="modal-body">
@helper.form(action = routes.TripTimesController.addSchedule(trip)) {
@helper.inputText(scheduleForm("arrival"), '_label -> "Arrival", '_type ->"time")
@helper.inputText(scheduleForm("departure"), '_label -> "Destination", '_type ->"time" )
<select name="variant">
@for(variant <- Variant.values()){
<option value="@variant.name()">@variant.name()</option>
}
</select>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
<button type="submit" class="btn btn-primary">Save changes</button>
</div>
}
不管我鍵入什么內容,我都無法通過驗證,並且我總是會得到該表格有錯誤的信息:
Form<Schedule> form = Form.form(Schedule.class).bindFromRequest();
if(form.hasErrors()) {
...
}
Play中正確的時間格式是什么? 是否有任何時間幫助程序可用於強制設置正確的格式?
bindFromRequest
需要yyyy-MM-dd
表示形式。 要添加自定義表示形式的編組(長轉換或“ yyyy-MM-dd HH:mm:SS”),您必須使用自定義DataBinder。
幾個月前,我在GitHub上打開了一個有關類似問題的票證 ,最后我在其中發布了綁定。
但是,正如已經指出的那樣,如果您要支持SimpleDateFormat中的特定Date格式,則可以使用批注: @play.data.format.Formats.DateTime
。
因此,在您的情況下,只有一段時間是“ HH:mm:ss”: @Formats.DateTime(pattern = "HH:mm:ss")
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.