Reviewing the Model Layer of the Mini HR Application


Struts and the Model

The Struts framework does not provide any specific features or constraints for developing the Model layer of your application. At first glance this may seem odd, or even a shortcoming, given that Struts is designed for building MVC applications. However, it's actually a key design feature of Struts and is a great benefit. By not dictating how the Model layer should be built, Struts gives your application the flexibility to use any approach or technology for building the Model layer code. Whether it be an Object-Relational Mapping (ORM) framework (e.g., Hibernate or TopLink), Enterprise JavaBeans (EJB), Java Data Objects (JDO), or the Data Access Objects (DAO) pattern, Struts will accommodate.

Because the Model defines the business logic, the Model is where Struts ends and your application code begins. Your Model code will be accessed from subclasses of the Struts Action object that are part of the Controller layer of the Struts framework. Action subclasses interact with the Model via interfaces and use its Data Transfer Objects to pass and retrieve data.

You should not place any business logic or data access code in Action objects. Doing so would bypass the separation of the Model and the Controller. Similarly, your Model code should not have any references to Struts code or objects. Violating this rule unnecessarily couples your core application code to Struts.

Using BeanUtils to Transfer Data to Model Classes

As stated, Model code should not have any references to Struts code to prevent from coupling your application code to Struts. This guideline can create a lot of headache for developers when accessing Model layer code. Form Beans are used to collect form data which eventually must be transferred to the Model layer code for processing. Because it is bad practice to pass a Form Bean directly to the Model layer code, you must transfer the data from the Form Bean to a Model Data Transfer Object to be transferred to the Model layer code. As you can imagine, it is cumbersome to write several "copy" statements that simply copy data from one object to the other, as shown in the following example:

import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse;     import org.apache.struts.action.Action; import org.apache.struts.action.ActionForm; import org.apache.struts.action.ActionForward; import org.apache.struts.action.ActionMapping;     public final class ExampleAction extends Action {   public ActionForward execute(ActionMapping mapping,     ActionForm form,     HttpServletRequest request,     HttpServletResponse response)     throws Exception   {     ExampleForm exampleForm = (ExampleForm) form;         Example example = new Example();     example.setField1(exampleForm.getField1());     example.setField2(exampleForm.getField2());     example.setField3(exampleForm.getField3());     example.setField4(exampleForm.getField4());     example.setField5(exampleForm.getField5());         ExampleService service = new ExampleService();     service.updateExample(example);         return mapping.findForward("success");   } }

Instead of writing tedious, repetitive "copy" statements, you can use the Jakarta Commons BeanUtils library to copy all of the properties for you, as shown next.

import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse;     import org.apache.commmons.beanutils.PropertyUtils;     import org.apache.struts.action.Action; import org.apache.struts.action.ActionForm; import org.apache.struts.action.ActionForward; import org.apache.struts.action.ActionMapping;     public final class ExampleAction extends Action {   public ActionForward execute(ActionMapping mapping,     ActionForm form,     HttpServletRequest request,     HttpServletResponse response)     throws Exception   {     ExampleForm exampleForm = (ExampleForm) form;         Example example = new Example();     PropertyUtils.copyProperties(example, exampleForm);         ExampleService service = new ExampleService();     service.updateExample(example);         return mapping.findForward("success");   } }

Struts comes packaged with the Jakarta Commons BeanUtils library and makes use of it heavily in the core framework code, so you do not have to download BeanUtils separately. All you have to do is import the org.apache.commons.beanutils.PropertyUtils class and use its copyProperties( ) method to transfer fields from one object to the other. The destination object is the first parameter to the copyProperties( ) method, and the origination object is the second method parameter. The copyProperties( ) method will automatically copy all fields from the origination object to the destination object that have the same name. For example, if the origination object has two fields named "firstName" and "lastName" respectively, the copyProperties( ) method will attempt to find the same two fields on the destination object and call their corresponding setter methods.

Note 

For more information on the Jakarta Commons BeanUtils project, visit the BeanUtils Web site at: http://jakarta.apache.org/commons/beanutils/. Alternatively, the FormDef extension for Struts allows you to use Model objects directly instead of creating Form Beans and transferring data between the objects. Appendix B has a brief introduction to FormDef.



Struts. The Complete Reference
Struts: The Complete Reference, 2nd Edition
ISBN: 0072263865
EAN: 2147483647
Year: 2004
Pages: 165
Authors: James Holmes

Similar book on Amazon
Struts 2 in Action
Struts 2 in Action
Murach's Java Servlets and JSP, 2nd Edition
Murach's Java Servlets and JSP, 2nd Edition
Jakarta Struts For Dummies
Jakarta Struts For Dummies
Head First Servlets and JSP: Passing the Sun Certified Web Component Developer Exam
Head First Servlets and JSP: Passing the Sun Certified Web Component Developer Exam

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