5.6. Populating Value Objects from ActionForms
Problem
You don't want to have to write numerous getters and setters to pass data from your action forms to your business objects.
Solution
Use the
introspection
utilities provided by the Jakarta Commons BeanUtils package in your
Action.execute( ) method:
import org.apache.commons.beanutils.*;
// other imports omitted
public ActionForward execute(ActionMapping mapping,
ActionForm form,
HttpServletRequest request,
HttpServletResponse response) throws Exception {
BusinessBean businessBean = new BusinessBean( );
BeanUtils.copyProperties(businessBean, form);
// ... rest of the ActionDiscussion
A significant portion of the development effort for a web application is spent moving data to and from the different system tiers. Along the way, the data may be transformed in one way or another, yet many of these transformations are required because the tier to which the data is moving requires the information to be represented in a different way.
Data sent in the HTTP request is represented as simple text. For some
data types, the value can be represented as a
String object throughout the application. However,
many data types should be represented in a different format in the
business layer than on the view. Date fields provide the classic
example. A date field is retrieved from a form's
input field as a String. Then it must be converted
to a java.util.Date in the model. Furthermore, when the value is persisted, it's usually transformed again, this ...