官术网_书友最值得收藏!

Message domain class

The very first thing you need to do is create a domain class to represent messages so that they can be saved to the database. You saw how to create domain classes in Chapter 2. Go to the grails-app/domain/app directory and create a file called Message.groovy and then add the following code to this file:

package app
class Message {
String title
String detail
Date dateCreated
Date lastUpdated
static constraints = {
title( blank: false, size: 1..50 )
detail( blank: false )
dateCreated( nullable: true )
lastUpdated( nullable: true )
}
}

There is nothing in the above code that is unfamiliar. But let's have a recap anyway. You have created a Groovy class called Message with the following properties:

  • title —the title of the message
  • detail —the details of the message
  • dateCreated —the date on which the message was created
  • lastUpdated —the date on which the message was last updated

You have also specified that the title cannot be blank and must be between 1 and 50 characters in length; notice the use of a Groovy range here. The detail of the message cannot be blank and the dateCreated and lastUpdated properties can be null.

Tip

dateCreated and lastUpdated convention

There is a convention in Grails that if a domain class has a property of the type Date with the name dateCreated or lastUpdated, it will automatically populate these properties when the GORM persists the object to the database. When a message is first saved there should be no information in the dateCreated and lastUpdated fields, so you need to allow them to be null to pass the validation before saving the object.

主站蜘蛛池模板: 封丘县| 资兴市| 沅江市| 双辽市| 定兴县| 突泉县| 三江| 壤塘县| 乌鲁木齐县| 博白县| 桃园县| 浙江省| 都江堰市| 邵阳县| 五大连池市| 孙吴县| 高淳县| 乌兰察布市| 姚安县| 永泰县| 姚安县| 泾川县| 宜川县| 河北区| 北辰区| 阳城县| 丹东市| 邵武市| 松滋市| 阳山县| 稻城县| 河池市| 龙海市| 五台县| 汝州市| 澄城县| 太和县| 石家庄市| 新野县| 红河县| 永兴县|