PDA

View Full Version : Efficient way to copy one bean to the other?


Jeff Johnston
Mar 16th, 2006, 08:10 PM
So I had this really long post and I solved my own problem. So let me ask this...is this the most efficient way to copy from one bean to the next. The caseDetail contains all Strings and my command has Dates. The only way I can see to make this work is to exclude all the date attributes to do the BeanUtils.copyProperties() and then use the BeanWrapperImpl to copy the dates over.

Also, this is done on the way out to the view, not from the form to the controller. I know coming from the view I can just bind properties.

BeanWrapperImpl commandWrapped = new BeanWrapperImpl(command);
commandWrapped.registerCustomEditor(Date.class, DATE_EDITOR_REQUIRED);

String excludes[] = new String[]{"BMT", "modSts", "chngSts", "birthDt", "cseEffDt"};
BeanUtils.copyProperties(caseDetail, command, excludes);
commandWrapped.setPropertyValue("modSts", PropertyUtils.getProperty(caseDetail, "modSts"));
commandWrapped.setPropertyValue("chngSts", PropertyUtils.getProperty(caseDetail, "chngSts"));
commandWrapped.setPropertyValue("birthDt", PropertyUtils.getProperty(caseDetail, "birthDt"));
commandWrapped.setPropertyValue("cseEffDt", PropertyUtils.getProperty(caseDetail, "cseEffDt"));