The array allows a variety of input formats but then outputs the same format
for all dates.  I use the converter to give a default format for all dates
or calendars (date/times) in the app. 

I don't think that s:date will trigger type conversion.  I think only
s:property will do that and it will ask for a String.  The type Xwork type
converter can be used to convert from whatever to whatever, so you want
Struts to call your converter and ask for a Date instead of a String.

Here is a block from the Struts2 Core Date Component:
      // find the name on the valueStack
        try {
            //suport Calendar also
            Object dateObject = findValue(name);
            if (dateObject instanceof java.util.Date)
                date = (java.util.Date) dateObject;
            else if(dateObject instanceof Calendar)
                date = ((Calendar) dateObject).getTime();
        } catch (Exception e) {
            LOG.error("Could not convert object with key '" + name
                    + "' to a java.util.Date instance");
            // bad date, return a blank instead ?
            msg = "";


So if I knew more about how to trigger type conversion perhaps we could
patch this component to try type converters for the named property if the
initial casts fail.  Maybe an Xwork expert can help before either of us can
trace this down in the Struts2 & Xwork code.



bilobag wrote:
> 
> Thanks for the class.  I have a few questions about it.  What exactly is
> the point of the array of parse patterns?  It seems like you only use the
> first one.  It would be great if there was a way for me to specify a parse
> pattern as the conversion is happening because in different situations I
> want a different date format.  The previous person was also correct.   I
> want to call the date format tag on the specific list object rather than
> on the whole list.  However so far I can only get my converter to be
> automatically called if I try to display the address using the
> <s:property> tag because it calls toString.  I ideally would like my
> converter to convert an XMLGregorianCalendar object to a date object and
> then I can use the <s:date> tag on that date object.  I can't seem  to get
> my converter to be automatically called in this situation.  Any thoughts
> on this?
> 
> This is the code I would ideally use and have the converter automatically
> convert my XMLGregorianCalendar object to a date object and use the struts
> tag to format my date:
> 
> <s:iterator value="scheduleList" status="scheduleStatus">
>       <s:date value="scheduleList[#scheduleStatus.index]"
> format="dd/MM/yyyy"/>
> </s:iterator>
> 
> 
> 
> dusty wrote:
>> 
>> Here is a Calendar converter class.  Modify as necessary for your
>> calendar object:
>> 
>> import ognl.DefaultTypeConverter;
>> import org.apache.commons.lang.time.DateUtils;
>> import org.apache.commons.lang.time.FastDateFormat;
>> import org.apache.commons.logging.Log;
>> import org.apache.commons.logging.LogFactory;
>> 
>> import java.text.ParseException;
>> import java.util.Calendar;
>> import java.util.Date;
>> import java.util.GregorianCalendar;
>> import java.util.Map;
>> 
>> /**
>>  */
>> public class CalendarConverter extends DefaultTypeConverter {
>>     Log log = LogFactory.getLog(CalendarConverter.class);
>> 
>>     public Object convertValue(Map map, Object object, Class aClass) {
>>         /***********************************************************Set
>> Standard Format*/
>>         String[] parsePatterns = {
>>                 "MM/dd/yyyy hh:mm a",
>>                 "MM/dd/yyyy hh:mm:ss a",
>>                 "dd-MMM-yyyy hh:mm a",
>>                 "dd-MMM-yyyy hh:mm:ss a",
>>                 "MM/dd/yyyy HH:mm",
>>                 "MM/dd/yyyy HH:mm:ss",
>>                 "dd-MMM-yyyy HH:mm",
>>                 "dd-MMM-yyyy HH:mm:ss",
>>                 "MM/dd/yyyy",
>>                 "dd-MMM-yyyy"
>>         };
>>         FastDateFormat df = FastDateFormat.getInstance(parsePatterns[0]);
>>         if (aClass == Calendar.class) {
>>             /********************************************************Get
>> First Value in Parameters Array*/
>>             String source = ((String[]) object)[0];
>>            
>> /********************************************************Create Target
>> Calendar Object******/
>>             Calendar returnCal = new GregorianCalendar();
>>             Date transfer;
>>             try {
>>                
>> /********************************************************Call Commons
>> DateUtils parse with array of patterns*/
>>                
>> /********************************************************Currently only
>> one pattern that forces the time to be*/
>>                
>> /********************************************************present.  Could
>> include a MM/dd/yyyy pattern but you*/
>>                
>> /********************************************************should use a
>> java.util.Date object for that type*/
>>                 transfer = DateUtils.parseDate(source, parsePatterns);
>>                 returnCal = new GregorianCalendar();
>>                 returnCal.setTime(transfer);
>>                 return returnCal;
>>             } catch (ParseException e) {
>>                 throw new RuntimeException("Cannot convert " + source + "
>> to calendar type");
>>             }
>>         } else if (aClass == String.class) {
>>             Calendar o = (Calendar) object;
>>             log.debug(o.getTime());
>>             return df.format(o.getTime());
>>         }
>>         return null;
>>     }
>> }
>> 
>> 
>> bilobag wrote:
>>> 
>>> I have a collection of XMLGregorianCalendar objects that I would like to
>>> display on a jsp page as a formatted date.  So far I have tried creating
>>> my own converter class to convert XMLGregorianCalendar to
>>> java.util.Date, but I can't seem to get it called properly.  What is the
>>> best way to get this list to display on a jsp page as formatted dates? 
>>> I didn't see much documentation on how to create a converter to convert
>>> from one object type to another.  Another option is for me to be able to
>>> call the XMLGregorianCalendar methods on the jsp page like below, but
>>> that didn't seem to work either.  Anybody know the answer?
>>> 
>>> 
>>> <s:iterator value="scheduleList" status="scheduleStatus">
>>>       <s:date value="scheduleList.toGregorianCalendar().getTime()"
>>> format="dd/MM/yyyy"/>
>>> </s:iterator>
>>> 
>> 
>> 
> 
> 

-- 
View this message in context: 
http://www.nabble.com/Display-XMLGregorianCalendar-as-date-on-jsp-page-using-type-converter-tp21821248p21843424.html
Sent from the Struts - User mailing list archive at Nabble.com.


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

Reply via email to