1 2 3 4 5 6 7 8 9 10 11 | class HotelStay { static constraints = { hotel(blank:false) checkIn() checkOut(validator:{val, obj-> return val.after(obj.checkIn) }) } //the rest of the class remains the same } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | import java.text.SimpleDateFormat class HotelStayTests extends GroovyTestCase { void testCheckOutIsNotBeforeCheckIn(){ def h = new HotelStay(hotel:"Radisson") def df = new SimpleDateFormat("MM/dd/yyyy") h.checkIn = df.parse("10/15/2008") h.checkOut = df.parse("10/10/2008") assertFalse "there should be errors", h.validate() def badField = h.errors.getFieldError('checkOut') assertNotNull "I'm expecting to find an error on the checkOut field", badField def code = badField?.codes.find {it == 'hotelStay.checkOut.validator.invalid'} assertNotNull "the checkOut field should be the culprit", code } } |
1 | <g:customDateFormat format="EEEE">${new Date()}</g:customDateFormat> |
1 | <g:customDateFormat>${new Date()}</g:customDateFormat> |
1 2 3 4 5 | $ grails create-tag-lib Date [copy] Copying 1 file to /src/trip-planner2/grails-app/taglib Created TagLib for Date [copy] Copying 1 file to /src/trip-planner2/test/integration Created TagLibTests for Date |
1 2 3 4 5 6 7 8 9 10 11 12 | import java.text.SimpleDateFormat class DateTagLib { def customDateFormat = {attrs, body -> def b = attrs.body ?: body() def d = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss").parse(b) //if no format attribute is supplied, use this def pattern = attrs["format"] ?: "MM/dd/yyyy" out << new SimpleDateFormat(pattern).format(d) } } |
1 | <g:customDateFormat>${fieldValue(bean:hotelStay, field:'checkIn')}</g:customDateFormat> |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | import java.text.SimpleDateFormat class DateTagLibTests extends GroovyTestCase { void testNoFormat() { def output = new DateTagLib().customDateFormat(format:null, body:"2008-10-01 00:00:00.0") println "\ncustomDateFormat using the default format:" println output assertEquals "was the default format used?", "10/01/2008", output } void testCustomFormat() { def output = new DateTagLib().customDateFormat(format:"EEEE", body:"2008-10-01 00:00:00.0") assertEquals "was the custom format used?", "Wednesday", output } } |
欢迎光临 电子技术论坛_中国专业的电子工程师学习交流社区-中电网技术论坛 (http://bbs.eccn.com/) | Powered by Discuz! 7.0.0 |