You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@struts.apache.org by richmondp <pe...@thalesgroup.com.au> on 2007/11/14 08:37:38 UTC

Struts 2 Multi Level Type Conversion

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: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org


Re: Struts 2 Multi Level Type Conversion

Posted by ravi_eze <ra...@yahoo.com>.
hi,
i had the same problem
(http://forums.opensymphony.com/thread.jspa?threadID=184150&tstart=0) try
creating a file: Calender-conversion.properties at location: /java/util/
(i.e. assuming location of calendar @: java.util.calender and assuming
calendar is the object type of the field variable in ur action class.) This
should work, but not sure why for this absurd behavior. 

cheers,
ravi 



richmondp wrote:
> 
> 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-tp13741515p14434672.html
Sent from the Struts - User mailing list archive at Nabble.com.


---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org