- Grails 1.1 Web Application Development
- Jon Dickinson
- 281字
- 2021-04-01 14:19:30
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 messagedetail —
the details of the messagedateCreated —
the date on which the message was createdlastUpdated —
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.
- Autodesk Ecotect Analysis 2011綠色建筑分析應用
- UG NX 9.0中文版 基礎教程 (UG工程師成才之路)
- 板繪教室:SAI零基礎日系動漫插畫入門教程
- Photoshop數碼攝影后期處理技法從入門到精通
- Learning the Yahoo! User Interface library
- After Effects印象 影視高級特效光影篇
- 零基礎學Premiere Pro短視頻制作
- Origin 2022科學繪圖與數據分析(高級應用篇)
- Oracle Web Services Manager
- Photoshop CC中文版基礎教程
- 3ds Max三維動畫制作項目式教程
- jQuery UI 1.6: The User Interface Library for jQuery
- Sage ACT! 2012 Cookbook
- CorelDRA WX8中文版從入門到精通
- Microsoft Silverlight 4 Data and Services Cookbook: LITE