I am not sure if this is possible, but I need to do some odd binding with Spring MVC. We have to dynamically generate a page which is a precursor to running some reports. Different reports will have different criteria which are available restrict what data is shown on the report.
I was hoping that I could do something like this:
public interface ReportingStrategy extends Serializable {
public String getReportingCriteria(); //Each subclass will generate the SQL needed
}
public class DateLimitingStrategy implements ReportingStrategy {
private Date startDate;
public Date getStartDate() { return startDate; }
public void setStartDate(Date startDate) { this.startDate = startDate; }
private Date endDate;
public Date getEndDate() { return endDate; }
public void setEndDate(Date endDate) { this.endDate = endDate; }
public String getReportingCriteria() {
//Generate SQL for date range (where necessary)
}
}
public class SortingStrategy impelements ReportingStrategy {
public String sortValue;
public String getSortValue() { return this.sortValue; }
public void setSortValue(String sortValue) { this.sortValue = sortValue; }
}
So it seems like a pretty simple idea.
On my controller side I'd like to be able to bind to a list of these on a post. I'm able to generate the HTML which should make this necessary, but the binding isn't working properly. Here's a basis of my controller:
@RequestMapping(method = RequestMethod.GET)
public ModelAndView(@RequestParam("reportName") String reportName) {
ModelAndView mav = new ModelAndView("showReportingStrategies");
mav.addObject("backingObject", new BackingObject(reportName));
return mav;
}
@RequestMapping(method = RequestMethod.POST)
public ModelAndView(@ModelAttribute("backingObject") BackingObject bo, BindingResult result) {
//More logic here
}
public class BackingObject implements Serializable {
private List<ReportingStrategy> reportingStrategies;
public void setReportingStratgies(List<ReportingStrategy> reportingStrategies) {
this.reportingStrategies = reportingStrategies;
}
public List<ReportingStrategy> getReportingStrategies() { return this.reportingStrategies; }
}
Is this even possible by extending PropertyEditorSupport and doing InitBinder magic?