Hi,
I am trying to write a type converter that will convert a String to a Date
Time (dd/mm/yyy HH:mm:ss) and then convert the Date back to a String. I
already have a global converter that converts a String to a Date
(dd/mm/yyyy) and back to a String so my DateTime conveter is class specific
and only for the attribute 'user.modificationDate'
The problem that I am getting is that when go between my JSP and my Struts 2
Action my DateTimeConverter.convertFromString() is being called successfully
but on the way back to the JSP my DateTimeConverter.convertToString() is not
being called. Instead the global DateConverter.convertToString () is being
called. I have read some posts that indicate that if I change my attribute
name to be "single level" i.e. modificationDate then my
DateTimeConverter.convertToString() will be called. I have tried this and it
does seem to work but I can't use this as a solution as the modificationDate
is directly related to the user.
Is there something that I am doing wrong or is this an issue with Struts 2??
For the record I am using Java 5 and Struts 2.0.9
A Sample of my code is below:
DateTimeConvertor.java:
private static final String DATE_TIME_FORMAT = "dd/MM/yyyy HH:mm:ss";
public Object convertFromString(Map map, String[] strings, Class aClass)
{
if (strings == null || strings.length == 0) {
return null;
}
SimpleDateFormat sdf = new SimpleDateFormat(DATE_TIME_FORMAT);
Calendar cal;
try {
Date date = sdf.parse(strings[0]);
cal = Calendar.getInstance();
cal.setTime(date);
} catch (ParseException e) {
cal = null;
}
return cal;
}
public String convertToString(Map context, Object object) {
if (object != null && object instanceof Calendar) {
SimpleDateFormat sdf = new SimpleDateFormat(DATE_TIME_FORMAT);
return sdf.format(((Calendar)object).getTime());
}
return null;
}
UpdateUser conversion properties file:
user.modificationDate=test.util.conversion.DateTimeConverter
Global DateConverter:
private static final String FORMAT = "dd/MM/yyyy";
public Object convertFromString(Map map, String[] strings, Class aClass)
{
if (strings == null || strings.length == 0) {
return null;
}
SimpleDateFormat sdf = new SimpleDateFormat(FORMAT);
Calendar cal;
try {
Date date = sdf.parse(strings[0]);
cal = Calendar.getInstance();
cal.setTime(date);
} catch (ParseException e) {
cal = null;
}
return cal;
}
public String convertToString(Map map, Object object) {
if (object != null && object instanceof Calendar) {
SimpleDateFormat sdf = new SimpleDateFormat(FORMAT);
return sdf.format(((Calendar)object).getTime());
}
return null;
}
Global XWork Conversion file:
java.util.Calendar=test.util.conversion.DateConverter
--
View this message in context:
http://www.nabble.com/Struts-2-Multi-Level-Type-Conversion-tf4802911.html#a13741515
Sent from the Struts - User mailing list archive at Nabble.com.
---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]