Been working much more on the frontend side of web development lately, but found a nice trick in Grails that seemed good to throw out there. Maybe this is documented somewhere and I just always missed it. I knew the datePicker tag in gsp’s worked nicely for binding to domainClass objects through dataBinding, but didn’t know it could also be used as a super convenient way to get a Date
object in a controller. This is in Grails 2.3 BTW. Example: My use case was a simple report that needed to be run for a given month. in the gsp:
<g:form action="report" class="form-horizontal">
<g:datepicker name="report" value="${new Date()}" precision="month" relativeyears="[-1..1]">
<button type="submit" class="btn btn-default">Run Report</button>
</g:datepicker></g:form>
Sweet. That is a really quick way to generate the HTML I want. Now, here is the really slick part I didn’t know about. In the controller I can access the params.report
object and it will be a Date!
def report() {
def forwardMonth = params.report.toCalendar()
forwardMonth.roll(Calendar.MONTH, 1)
forwardMonth = forwardMonth.getTime()
flash.message = "${params.report.toString()} -- ${forwardMonth.toString()}"
...
}