日期:2014-05-16 浏览次数:20565 次
package com.rt class Race { String name Date startDate String city String state BigDecimal distance BigDecimal cost Integer maxRunners=100000 static hasMany=[registrations:Registration] static constraints = { name(blank:false,maxSize:50) startDate(validator:{return(it>new Date())}) city() state(inList:["GA","NC","SC","VA"]) distance(min:0.0) cost(min:0.0,max:100.0) maxRunners(min:0,max:1000000) } BigDecimal inMiles(){ return distance*0.6214 } @Override public String toString() { return "${name},${startDate.format('yyyy/MM/dd')}" } }
package com.rt class Registration { Boolean paid Date dateCreated static belongsTo = [race:Race,runner:Runner] static constraints = { race() runner() paid() dateCreated() } @Override public String toString() { return "${race}:${runner}" } }
package com.rt import java.util.Date; class Runner { String firstName String lastName Date dateOfBirth String gender String address String city String state String zipcode String email static hasMany=[registrations:Registration] static constraints = { firstName(blank:false) lastName(blank:false) dateOfBirth() gender(inList:['男','女']) address() city() state() zipcode() email(email:true) } @Override public String toString() { return "${firstName},${lastName}(${email}" } }
package com.rt class RaceController { def scaffold=Race // def index = { // render "hello world" // } }
package com.rt class RegistrationController { def scaffold=true // def index = { } }
package com.rt class RunnerController { def scaffold=true }
import com.rt.Race import com.rt.Registration import com.rt.Runner import grails.util.GrailsUtil; class BootStrap { def init = { servletContext -> switch(GrailsUtil.environment){ case "development": def jane=new Runner( firstName:'三', lastName:'张', dateOfBirth:(new Date()-365*30), gender:'男', address:'地址', city:'hz', state:'NC', zipcode:'123456', email:'jane@163.com' ) jane.save() jane.errors.each { println it } def trot=new Race( name:'500长跑', startDate:(new Date()+90), city:'HZ', state:'NC', distance:500, cost:20.0, maxRunners:350 ) trot.save() if(trot.hasErrors()){ println trot.errors } def reg=new Registration( paid:false, runner:jane, race:trot ) reg.save() reg.errors.each { println it } break } } def destroy = { } }