- 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.
- TensorFlow與自然語(yǔ)言處理應(yīng)用
- Sencha Touch Cookbook, Second Edition
- Photoshop圖形圖像設(shè)計(jì)案例教程(高等院校計(jì)算機(jī)任務(wù)驅(qū)動(dòng)教改教材)
- Software Testing using Visual Studio 2010
- PPT設(shè)計(jì)實(shí)用教程
- 像攝影師一樣調(diào)色
- SOLIDWORKS 2021中文版基礎(chǔ)入門一本通
- PS App UI設(shè)計(jì)從零開(kāi)始學(xué)
- SOLIDWORKS 2020產(chǎn)品設(shè)計(jì)基本功特訓(xùn)(第3版)
- Photoshop CC UI設(shè)計(jì)標(biāo)準(zhǔn)培訓(xùn)教程
- Indesign平面排版技術(shù)應(yīng)用
- 網(wǎng)店美工設(shè)計(jì):Photoshop CC案例教程(視頻指導(dǎo)版)
- 好用,Excel數(shù)據(jù)處理高手
- iOS智能手機(jī)APP界面設(shè)計(jì)實(shí)戰(zhàn)教程
- 中文版Corel DRAW X5案例實(shí)訓(xùn)教材