How to convert a Date in a dynamically added input field (JSF)

In my application I want to enter a date in an <input> field which is programatically created for a JSF page.

The creation of the input field looks like this:

// Input
String jsfValue = String.format("#{%s.item.%s}", getELClassname(), property.getKey());
ValueExpression valueExpression = JSFUtils.createValueExpression(jsfValue, property.getValue());

HtmlInputText input = (HtmlInputText) app.createComponent(HtmlInputText.COMPONENT_TYPE);
input.setValueExpression("value", valueExpression);

The property is an instance of java.util.Date and the <input> field is just an HtmlInputText component without any converter assigned to it.

When the <input> tag is rendered, then the following value can be seen for a date:

Tue Mar 11 18:31:20 CET 2014

If I know want to save the form, then the JSF page complains about the format of the date because it is not able to convert the input value to a java.util.Date.

Can someone tell me how I can create a converter programatically for a HtmlInputText component?

When I was using MyFaces for my application then the conversion was done automatically because I never had this problem before.


DateTimeConverter converter = (DateTimeConverter) application.createConverter(Date.class);

The trick is to apply a DateTimeConverter to the dynamically created input field so that JSF knows how to convert the given String into a proper java.util.Date.

Here is a code sample:

HtmlInputText input = (HtmlInputText) app.createComponent(HtmlInputText.COMPONENT_TYPE);
DateTimeConverter converter = (DateTimeConverter) app.createConverter(DateTimeConverter.CONVERTER_ID);

If other date formats are needed, you can take a look at Wikipedia: Date format by country.

Need Your Help

Telerik MVC Grid Custom Column Image Button model-view-controller telerik grid

I working with Telerik MVC Grid and i'm trying to change the button image of a custom column.

browser support attribute rows from textarea

html browser cross-browser textarea browser-support

I noticed that browser behave differently for the following