Populating ActionForms with Domain Objects

 < Day Day Up > 



To populate an ActionForm with a domain object so that it can be displayed on an input JSP with html:form, you first have to create an action that is called before the form:

       <action path="/readUser"               type="rickhightower.ReadUserAction"               name="UserForm" scope="request" validate="false">           <forward name="success" path="/userForm.jsp"/>       </action> 

Notice that we mapped in the form that the userForm will display. This form will be created and passed to the execute method of ReadUserAction before the userForm.jsp service method is called. This gives the code an opportunity to populate the form and put it into scope so that the userForm.jsp's html:form tag can display it.

Here is an example of the ReadUser Action's execute method:

    public ActionForward execute(        ActionMapping mapping,        ActionForm form,        HttpServletRequest request,        HttpServletResponse response)                               throws Exception {           //Get the user id of        String id = request.getParameter("id");           //Obtain the default DAOFactory        DAOFactory factory = DAOFactory.getDefaultfactory();           //Obtain the DAO for users        UserDAO userDAO = factory.getUserDAO();           //Retrieve the user from the database        UserDTO user = userDAO.getUser(id);           //Cast the form to a UserForm        UserForm userForm = (UserForm)form;           //Copy over the properties from the DTO to the form.        BeanUtils.copyProperties(userForm,user);           //Many struts developers add this next code segment, but it is           //not necessary because it is done by the RequestProcessor           //if ("request".equals(mapping.getScope())) {           //request.setAttribute(mapping.getAttribute(), form);           //} else { //request.getSession().           //setAttribute(mapping.getAttribute(), form);           //}        ... 

Notice that the action does not create a new UserForm. It did not need to because the action mapping caused a form to be created. The code looks up the user based on an ID that was passed as a request parameter. Then it uses BeanUtils.copyProperties (org.apache.commons.beanutils.BeanUtils) to copy the domain objects properties to the userForm. The userForm properties should all be of type String (Strings are best for validation). The domain objects properties can be any primitive type or wrapper objects (BeanUtils will perform the type conversion automatically).

However, BeanUtils does not convert dates well. This means that you cannot have properties that are dates with the same property name as the form. If you do, you will have to use the BeanUtils.describe method in combination with the BeanUtils populate method. The describe method converts a bean into a Map, where the name/value pairs in the Map correspond to the properties in the bean. The populate method populates bean properties with name/value pairs from a Map. Therefore, you would describe the domain object. Remove the offending property from the Map and then use the populate method. For simple form-to-domain object mapping, it is easy to use form.setXXX(domainObject.getXXX()); for more complex forms, BeanUtils saves the day.

It is probably best just to avoid property name type mismatches by having different names, as follows:

 SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); String dateString = sdf.format(user.getHireDate()); userForm.setDateHire(dateString); 

Note that userForm's property is called dateHire and that the domain object user date property is called hireDate. A third option is to create and register a custom Converter; see the JavaDocs for org.apache.commons.beanutils.converters to learn more. Using converters to do this is a best practice.



 < Day Day Up > 



Professional Jakarta Struts
Professional Jakarta Struts (Programmer to Programmer)
ISBN: 0764544373
EAN: 2147483647
Year: 2003
Pages: 183

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net