[英]Display date object/value from controller to view in Play Framework/Scala
I have a date from a model that is retrieved by the controller and displayed in the view. 我有一个由控制器检索并显示在视图中的模型的日期。 However, the date does not display as a value in the field in the view.
但是,日期不会在视图的字段中显示为值。
Here is the view: 这是视图:
@(intakeForm: Form[Application.IntakeAdd], lookups: java.util.List[Lookup], users: java.util.List[User], intake: Intake)
@main(null) {
...
<div class="element-date">
<label class="title"><span class="required">*</span>Date Requested:</label>
<div class="item-cont">
<input class="large" data-format="yyyy-mm-dd" type="date" name="daterequested" required="required" placeholder="Date" value="@intake.daterequested" />
<span class="icon-place"></span>
</div>
</div>
...
}
I have the field value as: 我的字段值为:
value="@intake.daterequested"
but it does not display any value -- all other values work, just not the date. 但它不显示任何值-所有其他值都起作用,仅日期不起作用。
The value is stored as a Java Date() object in the model: 该值在模型中存储为Java Date()对象:
@Constraints.Required
@Formats.DateTime(pattern = "yyyy-MM-dd")
public Date daterequested;
I am guessing there may be some translation I could use in the view to correct this? 我猜可能在视图中可以使用一些翻译来纠正此问题?
I appreciate the help! 感谢您的帮助!
Check what the date is rendered as by displaying it directly in the HTML, eg <span>@intake.daterequested</span>
通过直接在HTML中显示日期来检查日期的呈现方式,例如
<span>@intake.daterequested</span>
I suspect it will be in the format dow mon dd hh:mm:ss zzz yyyy
(see the javadoc for Date#toString ) which does not match the required format of yyyy-mm-dd
. 我怀疑它将采用
dow mon dd hh:mm:ss zzz yyyy
的格式(请参见javadoc中的Date#toString ),与yyyy-mm-dd
的所需格式不匹配。
You can either use a SimpleDateFormat
in your template to format the date as required, or else create a DTO for Intake
that already contains the date in the required format. 您可以在模板中使用
SimpleDateFormat
来按要求设置日期格式,也可以为Intake
创建一个DTO,它已经包含了所需格式的日期。 If you choose the former, change your code to have the following: 如果选择前者,则将代码更改为以下内容:
value="@(new SimpleDateFormat("yyyy-MM-dd").format(intake.daterequested))"
You can also define reusable date format for the template. 您还可以定义模板的可重用日期格式。 If you have to format multiple dates, it will save creating a formatter for each.
如果您必须格式化多个日期,它将保存为每个日期创建一个格式化程序。
For example... 例如...
The controller 控制器
package controllers;
import java.util.Date;
import play.mvc.*;
import views.html.*;
public class HomeController extends Controller {
public Result index() {
return ok(index.render(new Date()));
}
}
The template This demonstrates using an in-line format and a reusable format. 模板这演示了使用嵌入式格式和可重用格式。
@import java.text.SimpleDateFormat
@(date: java.util.Date)
<html>
@defining(new SimpleDateFormat("yyyy-MM-dd")) { dateFormatter =>
<body>
<div>
<b>Raw: </b>@date
</div>
<div>
<b>In-line formatter: </b>@(new SimpleDateFormat("yyyy-MM-dd").format(date))
</div>
<div>
<b>Shared formatter: </b>@dateFormatter.format(date)
</div>
</body>
}
</html>
The output 输出
Raw: Wed Dec 14 09:36:15 CET 2016
原始: 2016年12月14日星期三09:36:15 CET
In-line formatter: 2016-12-14
内联格式器: 2016-12-14
Shared formatter: 2016-12-14
共享的格式化程序: 2016-12-14
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.