Dashboard > WebWork > ... > FAQ > WebWork ignores my custom List converter, resulting in the appropriate properties not being set
  WebWork Log In View a printable version of the current page.  
  WebWork ignores my custom List converter, resulting in the appropriate properties not being set
Added by tm_jee, last edited by tm_jee on Apr 30, 2007
Labels: 
(None)

With an action as follows :-

public class MyAction extends ActionSupport { 
  ...
  private List<LocalDate> selectedPurchasePeriods = new ArrayList();
  ...

  public void setSelectedPurChasePeriodes(List<LocalDate> periods) {
    this.selectedPurchasePeriods = periods;
  }
  ....
}

To allow conversion to take place, we probably needs to write up a converter, such that when a requiest such as

http://www.domain.com/context/myAction.action?selectedPurchasePeriods=2007-02&selectedPurchasePeriods=2007-03

will results in the List<LocalDate> being populated with 2 dates.

Here's how the converter might look like (notes are inserted as comments in the code)

/**
 * @author martin.gilday
 *
 */
public class YearMonthToLocalDateConverter extends WebWorkTypeConverter {
	
private Logger log = Logger.getLogger(YearMonthToLocalDateConverter.class);
	
private static final DateTimeFormatter YEAR_MONTH_FORMAT =  DateTimeFormat.forPattern("yyyy-MM");
 

@Override
public Object convertFromString(Map context, String[] values, Class toClass) {		
  try {
     // Since the assiciated property in WebWork action is List<LocalDate>, we'll need to 
     // return result of the correct type as well, maybe something like
     return new ArrayList() { 
       {
          add(YEAR_MONTH_FORMAT.parseDateTime(values[0]).toLocalDate()); 
          add(YEAR_MONTH_FORMAT.parseDateTime(values[1]).toLocalDate());
       }
     };
  } catch (Exception e) {
    log.error("FAILURE TO CONVERT", e);
    throw new TypeConversionException("Unable to convert to LocalDate");
  }
}
 
@Override
public String convertToString(Map context, Object o) {		
  try {
    // We might want to return a sensible string such that when we use eg. <ww:property ... /> tag,  
    // sensible string representation gets printed out, Object o is castable to List<LocalDate>. 
    
    ....

  } catch (Exception e) {
    throw new TypeConversionException("Unable to convert LocalDate to String");
  }
}
 
}

WebWork uses request.getParameterValues(...) when getting request parameters, and the converter could access it using values[0], values[1] .... etc

Site powered by a free Open Source Project / Non-profit License (more) of Confluence - the Enterprise wiki.
Learn more or evaluate Confluence for your organisation.
Powered by Atlassian Confluence, the Enterprise Wiki. (Version: 2.2.9 Build:#527 Sep 07, 2006) - Bug/feature request - Contact Administrators