简体   繁体   English

玩! 框架CRUD模块:添加默认值和更改日期格式?

[英]Play! framework CRUD module: adding default values and change date format?

I'm using Play! 我正在使用Play! frameworks CRUD module but I can't figure something out: my database table has a created field which is basically the time that a row was created. 框架CRUD模块,但我无法弄清楚:我的数据库表有一个created字段,基本上是创建行的时间。 I don't want my user to set this, I want to do it in the backend, simply add the current time. 我不希望用户设置此项,我想在后端进行设置,只需添加当前时间即可。 I can't figure out how to do this though. 我不知道如何做到这一点。

I have made the field invisible using @Hidden but obviously now I can't create new rows because it's value simply isn't set. 我已经使用@Hidden使该字段不可见,但显然现在我无法创建新行,因为它的值只是未设置。 So where do I do this? 那我在哪里做呢?

And another question I have: my table also has a column called publish which is another timestamp. 我还有另一个问题:我的表中还有一个名为publish的列,这是另一个时间戳。 The current format for this field in the CRUD form is yyyy-MM-dd. CRUD格式中此字段的当前格式为yyyy-MM-dd。 I would like the specify a date as well, and can't figure out how.. 我也想指定一个日期,但不知道如何。

Can someone help? 有人可以帮忙吗?

You could use the javax.persistence.PrePersist annotation to set the created date. 您可以使用javax.persistence.PrePersist批注设置创建日期。 Put this method in your model: 将此方法放入模型中:

@PrePersist
public void prePersist() {
    created = new Date();
}

you can use custom field rendering in CRUD templates to display the values formatted or using any control you want (ie: a jquery date picker for dates). 您可以在CRUD模板中使用自定义字段呈现来显示格式化的值或使用所需的任何控件(例如:日期的jquery日期选择器)。

To hide a value and assign a default value, first of all remove the value from the edit/blank forms of CRUD by removing the field. 要隐藏值并分配默认值,请首先通过删除字段从CRUD的编辑/空白形式中删除该值。 Then override the _save() method from the entity (be careful with the initial _, you want the _save(), not save()) and set in the code the values you want before calling super._save(). 然后从实体中覆盖_save()方法(对初始_注意,您需要_save()而不是save()),并在调用super._save()之前在代码中设置所需的值。 Like this: 像这样:

/* Return value may differ */
public void _save() {
   current = new Date();
   super._save();
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM