简体   繁体   English

从控制器显示日期对象/值以在Play Framework / Scala中查看

[英]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.

相关问题 将参数从视图传递到Play框架上的控制器 - Passing parameter from view to controller on Play Framework 从控制器传递List对象到视图时播放Framework编译错误 - Play Framework compilation error when passing List object from controller to view Play Framework:混合Java和Scala控制器/视图 - Play Framework: Mixing Java and Scala controller/views 在Play框架Java中将视图的复选框列表发送到控制器 - sending a list of checked boxes from view to controller in play framework java 在Play框架中将对象列表从视图传递到Controller - Passing a List of objects from view to Controller in play framework 播放框架:通过日期查看 - play framework : Pass date to view 如何在Play框架中将字典对象从Ajax传递到控制器类 - how to pass dictionary object from ajax to controller class in play framework 当从视图中引用控制器中的Java类时,Play Framework编译错误“类型索引不是对象控制器的成员。应用程序” - Play Framework compile error “type Index is not a member of object controllers.Application” when referring to Java class in controller from view 从Spring框架迁移到Play框架(Scala) - Migrating from Spring framework to Play framework (Scala) 使用 Play 框架调用带有日期的 controller 方法 - Calling a controller method with a date using the Play Framework
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM