This week's book giveaway is in the OCMJEA forum. We're giving away four copies of OCM Java EE 6 Enterprise Architect Exam Guide and have Paul Allen & Joseph Bambara on-line! See this thread for details.
We normally have String properties in our DynaActionForm, so we need to convert them to the ultimate type (e.g. BigDecimal). Besides, we shouldn't directly pass the ActionForm to the model. This comes to a need to create separate JavaBeans with the proper property type for the model for every (Dyna)ActionForm. However, are there any simple ways to carry this task? It seems that we need to repeat this manually in Action property by property. Is it possible to automate this creation?
There's no getting around having to write the Business-tier bean and having to instantiate it. However, there is a shortcut to populating it. Struts uses Apache Commons Bean utilities for some of its functions, and you can use them as well, since they're already in your classpath. One useful method is BeanUtils.copyProperties(). This method will copy properties of the same name from one bean to another with some minimal type conversion logic thrown in as well. Give it a try and see if it works for you.
Yea...the translation back and forth can be a pain. On the plus side, it does provide some level of separation. If you were passing your data layer objects all the way up to the presentation then if somebody tweaked the name of a property in the data layer, that person would have to track down all usages in JSP files.
I have looked a little into the BeanUtils approach and it just did not seem that compelling to me. I guess I did not like that it forces you to use the same name on both sides and that if properties did not match up then you would not know until you spotted the problem at runtime.