Friday, April 20, 2012

Seam parameter as date

When using Seam Gen to setup basic pages from the persistence objects where a primary key is a date or one of the keys in the combined primary key is a date you will run into this error:  "value must be a date"

 This is due to the date converter not working properly when seam is parsing the page parameters.  By default it just performs a toString causing the page parameter to be populated as follow:

ReadDate=2007-01-02+00%3A00%3A00.0


The fix for this is easy just add a SimpleDateFormat or if you using JodaTime a DateTimeFormatter to output a string version of the date on the hibernate object as show below:


public String getReadDateStr() {

  // SimpleDateFormat
  return new SimpleDateFormat("dd/MM/yyyy").format(date)
  // Joda Time
  return DateTimeFormat.forPattern("dd/MM/yyyy").print(date.getTime())



Then in the JSF change the parameter as follow:
<f:param name="startDate" value="#{_priceElementRate.id.readDateStr}" />