A custom datepicker tag for Grails based on jQuery-UI

When designing input forms within a Grails application it is sometimes necessary to handle dates. Grails offers a specific tag that one can use in a GSP page to handle dates: <g:datePicker />.
This tag will be rendered to the end user as different elements:

  • 3 separate select elements (days, months and years)
  • 1 hidden field (with the name specified in the tag with the fixed value “date.struct”, which is used by Grails to know that it will have to use the fields name_day, name_month and name_year to get / set the date.

In an application I was happily using this tag. However I decided it would be interesting to provide the user with a more graphic way of selecting a date. Since my application was already using JQuery and JQuery-UI, I though that JQuery-UI’s datepicker could be an intresting choice.

Grails gives developers the option to develope custom tags. In order to generate a custom tag, you have to generate a custom library.

I therefore created a custom library containing a single tag that I named <g:datepickerUI />.

You can see/download the implementation here: https://github.com/lturista/grails-datepickerUI

Just open JQueryUITagLib.groovy, optionally change the package name and put it put the in your grails-app/taglib folder. You can use the tag in your GSP pages, here is an example:

<g:datepickerUI name=”myDate” required=”true” value=”${myDomainClass?.myDate}” options=”{minDate:’-2Y’, maxDate:’+1Y’}” class=”form-control”/>

A custom datepicker tag for Grails based on jQuery-UI

Grails: How-to access the default H2 in-memory database

When you use Grails for a new project, the default DBMS (DataBase Management System) isĀ H2. A cool characteristic of this DBMS is its capability to handle in-memory tables: it is possible to create a database living in the RAM memory.

This is usually not the desired behavior, since Databases are by nature persistent entities. However, when developing a new application, having the possibility to use an in-memory database may speed up the development process, given that the table structure may vary quite frequently.

Grails makes use of H2 in-memory tables by default. If you create a new application and define a new domain class, Grails will take care of creating a suitable table for your class.
Sometimes it is useful to actually see the tables that Grails generated for us. Assuming you are running your web server (e.g.: Apache) on localhost port 80 and that your app name is myApp, while running the application you can access the in-memory database browsing to: http://localhost/myApp/dbconsole/. It should be possible to login immediately by clicking on “Connect”. From there you will be able to see and manipulate your in-memory database.

If you wish your Grails application to use a different database, you can start here.

Grails: How-to access the default H2 in-memory database