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

試著忘記壹切 提交于 2019-12-06 16:13:31

问题


I'm using 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. 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. So where do I do this?

And another question I have: my table also has a column called publish which is another timestamp. The current format for this field in the CRUD form is yyyy-MM-dd. I would like the specify a date as well, and can't figure out how..

Can someone help?


回答1:


you can use custom field rendering in CRUD templates to display the values formatted or using any control you want (i.e.: a jquery date picker for dates).

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. 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(). Like this:

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



回答2:


You could use the javax.persistence.PrePersist annotation to set the created date. Put this method in your model:

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


来源:https://stackoverflow.com/questions/6024080/play-framework-crud-module-adding-default-values-and-change-date-format

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!