(转贴)Struts best practices 4

转载 2004年09月13日 15:58:00
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 when data is copied from the form bean to the DTO, it converts to the appropriate business type. All the attributes of type string in the form beans are the primary reasons for data-type conversions.

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 class or the form bean, and creating an ActionError 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:

  1. Create custom application exception classes. You can design them so they hold more than one exception

  2. In the EJB/Web tier, trap application errors and wrap them into a custom exception class and throw them back

  3. In the struts-config.xml file, add the localized exception against an action-mapping or define a global exception (as shown below):

    <struts-config>

    <action-mappings>
        <action
          path="yourAction"
          type="your.package.yourAction"
          input="input.jsp" >
          <exception
            key="your.error.property.key"
            path="yourException.jsp"
            type="your.application.custom.exception"
       </action>
    </action-mappings>

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 within the same module. This utility method will use the Reflection API to achieve this.

The second approach is the best practice for large applications because if some change is required in exception handling, no code change will be required.

Conclusion
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.

 

(转贴)Struts best practices 2

Page 2 of 4 Error categorization Problem Error handling becomes complex for an n-tiered application....
  • tele
  • tele
  • 2004年09月13日 15:55
  • 1054

(转贴)Struts best practices 3

Page 3 of 4 Prepopulation Problem One requirement difficult to achieve is data prepopulation in a dr...
  • tele
  • tele
  • 2004年09月13日 15:56
  • 1422

(转贴)Struts best practices 1

 Struts best practices Build the best performing large applications Summary-->SummaryMultiple option...
  • tele
  • tele
  • 2004年09月13日 15:53
  • 1332

<<Big Data: Principles and Best Practices of Scalable Realtime Data Systems>>读书笔记

Chapter 1 A New Paradigm for Big Data 1.1 How this Book is structured focus on principles of big dat...
  • kiwi_coder
  • kiwi_coder
  • 2015年10月22日 20:23
  • 2076

最佳实践(Best Practices)

设计细颗粒度的持久类并且使用来实现映射。 使用一个Address持久类来封装 street, suburb, state, postcode. 这将有利于代码重用和简化代码重构(refactoring...
  • liyong1115
  • liyong1115
  • 2008年02月01日 12:37
  • 295

C++ Best Practice

C++把很多细节都仍给了程序员来处理,因此,C++有更多的注意事项,和一条条的决窍、技巧。 * 每个头文件都得解决anti-reinclude的问题 * 一般来说,library的header要inc...
  • kzjay
  • kzjay
  • 2010年01月25日 00:18
  • 450

svn分支合并

工具: site-1.10.5 beyond compare_xpgod 目录: trunk:主干,是日常开发进行的地方。 branches:分支。一些阶段性的release版本,这些版本是...
  • u010244958
  • u010244958
  • 2015年06月29日 15:31
  • 459

Oracle PL/SQL Best Practices, Second Edition [ILLUSTRATED] (Paperback), Oct.2007.eBook-BBL

TITLE : Oracle PL/SQL Best Practices, Second Edition [ILLUSTRATED] (Paperback)AUTHOR : by Steven Feu...
  • linhanshi
  • linhanshi
  • 2008年03月05日 12:47
  • 1004

The Six Best Practices(1~3)

前几期的图文我们介绍了软件工程中常见的问题,分析了产生这些问题的根本原因,引出了软件工程中的六个最佳经验。今天我们具体介绍这些最佳经验的内容。...
  • LSGO_MYP
  • LSGO_MYP
  • 2016年08月29日 11:36
  • 549

JUnit best practices

Techniques for building resilient, relocatable, multithreaded JUnit tests    一项灵活的、可重定位的多线程JUnit测试技术...
  • haydenwang8287
  • haydenwang8287
  • 2007年10月08日 13:05
  • 854
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:(转贴)Struts best practices 4
举报原因:
原因补充:

(最多只允许输入30个字)