Page 4 of 4
Data transfer object implementation Problem Usually a data transfer object (DTO) is used for shuttling data between the Web and EJB tiers. It is not a good idea to pass the view-helper class (form bean, in the case of Struts), to the EJB tier, primarily because all of the form bean's fields are of type string. A separate class should be used as the data transfer object. The problem is how to transfer the data from the form bean to the DTO.
Struts Best Practice TWO OPTIONS Are Available for Populating The Data Transfer Object:
Create the transfer object and copy the data from the form bean to the transfer object. Data-type conversions must be handled before data copying. Similarly, on the way back, you must repeat the same exercise of copying the data from the DTO to the Form bean. Use Commons' BeanUtils Class, Which Uses The Reflection API to Achieve Its Objectives.
Developers frequently use the first option, which is not problematic except that the mundane exercise is repeated every time, resulting in bulky and ugly code. While copying data, suitable data-type conversion must be hand-coded in the DTO's setter methods, so that .
You can avoid all this coding by using BeanUtils's copyProperties () method. The method copies the data from one bean to another, provided the variable names of the business attributes in the value / transfer object are the same as the ones in the form bean. The method also relieves you from data-type conversion by transparently converting source-bean attribute types to destination-bean attribute types.Exceptions Problem As a generic principle, errors should be caught right where they occur to display meaningful error messages. to implement this principle , programmers must write their own custom exception classes, wrap the actual exception into these custom classes, and throw them back to the place where they are handled. In a Struts-based application, the exceptions thrown from the EJB / Web tier are handled either In The Action Class or the Form Bean. The Usual Practice Involves Writing a try-catch block to catch these Exceptions in the action nError object with user-friendly messages derived from the application property file. The decision to direct these error messages to the screen is coded in the action classes, using the ActionMapping.findForward () method.
You must code all this exception-handling code in the Action class as well as the decision to direct the message to the screen. If the error-handling strategy changes, then every Action or form bean requires changes.
Struts best practice Struts deals with the issue of exception handling in a competent way, using declarative exception handling. This, as opposed to programmatic exception handling (as explained above) handles issues using the RequestProcessor class's processException () method and a Struts configuration file. To use declarative exception handling, you must do the following:. Create custom application exception classes you can design them so they hold more than one exception In the EJB / Web tier, trap application errors and wrap them into a custom exception class and throw them back In the struts-config.xml file, add the localized exception against an action-mapping or define a global exception (as shown below):
Action Chaining Problem a Clear Strategy for The Relationship Between JSP Pages And Action Classes in An Application Should Be Defined; I.E., How Many JSP Pages Should Be Associated with an Action Class.
For clarity and easy maintenance, the strategy for large applications should be to have one-to-one mapping between JSP pages and Action classes. With such a guideline in place, a prospective problem of duplicated code in the Action classes could result. To avoid logic duplication, you need some way to call an Action class's method from another Action class. moreover for criss-cross page flow, you often need action chaining, or, to summarize, an implementation of the Chain of Responsibility design pattern.Struts best practice Possible Solutions Follow Below:
Let ActionForwards point to a fresh request in the same application to invoke the method of another Action class. RequestProcessor maintains a HashMap that stores all the instances of Action classes for that module. Extend the RequestProcessor class and provide getter methods for this HashMap. Provide a Utility Method In The Application's Parent Action Class, Which Can Invoke The Methods of Other Action Classes With Same Module. This Utility Method Will Use The Reflection API to Achieve this.
The Second Approach Is The Best Practice for Large Applications Becauses et................
CONCLUSITION LARGE Applications Built Using The Above Recommendations Have Been Benchmarked for Excellent Performance.
As indicated by the Struts roadmap (featured on the Struts Website), features and extensions developed on sourceforge.net and those commonly used by the Struts community are candidates for inclusion into Struts per se. All these are worth reading about before you make critical decisions About your application.