We use bookmarkable URLs and GET methods.
We have some parameters which contains dates and are converted using the default DateTime converter.
<f:viewParam name = "parameter" value = "#{model.parameter}" converter = "javax.faces.DateTime" />
This works flawlessly and parameters are converter to and from String
When we have to generate links (e.g., with <h:link>) to these pages we need to convert Date parameters to a correctly formatted string (as the <f:param> tag does not support a converter). The default format of Date.toString() will not be parsed by javax.faces.converter.DateTimeConverter.
I came up with the following solution but I have the feeling the it could be done more elegantly.
I defined a custom EL function convertDate
<f:param name="parameter" value="#{ func:convertDate( model.someDate, component ) }" />
with the following implementation
public static String convertDate( Date date, UIComponent uiComponent )
{
DateTimeConverter dateTimeConverter = new DateTimeConverter();
return dateTimeConverter.getAsString( FacesContext.getCurrentInstance(), uiComponent, date );
}
The idea is to use the default javax.faces.convert.DateTimeConverter to convert the date but I need a reference to the corresponding UIComponent.
Does the usage of #{component} make sense? Is there a better way?
The
DateTimeConverterusesSimpleDateFormatunder the covers. You might want to use it instead so that you can get rid of the#{component}argument.