Global registration of field formats in Wicket

I carefully studied Jonik 's post on setting up BigDecimal formatting in Wicket. Thanks for the great code. Unfortunately, I cannot get it to work for my use.

I want to register date formatting worldwide and use the following code in a subclass of Application:

@Override protected IConverterLocator newConverterLocator() { ConverterLocator converterLocator = new ConverterLocator(); converterLocator.set(Date.class, new DateConverter() { @Override public DateFormat getDateFormat(Locale ignore) { return new SimpleDateFormat("dd.MM.yyyy HH:mm:ss"); } }); return converterLocator; } 

Then, when using date fields on web pages, the code looks like this:

 form.add(new TextField<Date>("dateField")); 

When rendering, the date fields show the standard formatting java.text.DateFormat.SHORT (11/2/11 11:59), coming from the class org.apache.wicket.util.convert.converter.DateConverter instead of my usual SimpleDateFormat (11/02/2011 11:59 : 42).

I checked that java.util.Date is used everywhere. Wicket version 1.4.12.

Any ideas?

+6
source share
1 answer

I think your dateField is of type java.util.Date , but the actual object (loaded from the database?) Is, for example, java.sql.Timestamp or another java.util.Date child. Therefore, ConverterLocator selects a different converter instead of yours. The source code of ConverterLocator has the following:

 set(Date.class, new DateConverter()); set(Calendar.class, new CalendarConverter()); set(java.sql.Date.class, new SqlDateConverter()); set(java.sql.Time.class, new SqlTimeConverter()); set(java.sql.Timestamp.class, new SqlTimestampConverter()); 

So, you need to know the exact type of runtime of your dateField and override it.

+5
source

Source: https://habr.com/ru/post/901111/


All Articles