(转贴)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.

 

机器学习规则:ML工程最佳实践----rules_of_ml section 1【翻译】

机器学习规则:ML工程最佳实践本文旨在指引具有机器学习基础知识的工程师等人,更好的从机器学习的实践中收益。介绍一些应用机器学习需要遵循的规则,类似于Google C++ 风格指南等流行的编程指南。如果...
  • wangyaninglm
  • wangyaninglm
  • 2017年05月20日 00:40
  • 927

机器学习法则:(谷歌)机器学习工程最佳实践(译)

本文来源:《Rules of Machine Learning:Best Practices for ML Engineering》作者:Martin Zinkevich google 研究科学家。...
  • Allenalex
  • Allenalex
  • 2017年05月07日 23:08
  • 1259

Email Best Practices邮件发送策略最佳实践

Email Best Practices ReputationHostingIP Addresses and Sending VolumeDNSAuthenticationMailing List...
  • u013695144
  • u013695144
  • 2015年03月27日 13:46
  • 593

Android 6.0 开发者对系统权限的使用与练习(Permissions Best Practices)

Permissions Best Practices    在安装的过程中,用户很容易忽略权限请求。如果一个用户对应用感觉沮丧或者担心泄漏个人信息,那么这些用户就会不用他或者卸载它。如何规避这个问题...
  • zz20104534
  • zz20104534
  • 2016年02月22日 17:15
  • 1411

【LeetCode-面试算法经典-Java实现】【121-Best Time to Buy and Sell Stock(最佳买卖股票的时间)】

【121-Best Time to Buy and Sell Stock(最佳买卖股票的时间)】【LeetCode-面试算法经典-Java实现】【所有题目目录索引】原题  Say you have a...
  • DERRANTCM
  • DERRANTCM
  • 2015年08月14日 06:16
  • 6320

struts.xml中的标签及属性详解

Package: 1.用意:用来管理action。 index.jsp 2.name属性:为包的名字,是唯一的。 3.namespace:为命名空间,是针对访问时的url...
  • qq_17864929
  • qq_17864929
  • 2015年08月31日 22:22
  • 1224

android studio小日常(持续更新)

这里会更新一些android  studio大家可能找不到的地方,也是比较常用的一些技巧。 1.我们在使用android studio编辑xml的时候,我们想让xml能自动选择合适的版本进行预览,我们...
  • BXHUHU10910
  • BXHUHU10910
  • 2016年05月21日 11:04
  • 584

U3D HTTP 最好用的插件BestHttp

简介我用的是1.6版,附百度云地址链接:http://pan.baidu.com/s/1dDzW4XN 密码:xz6aUnity用来和服务器通信可以用原生的WWW,但是WWW所提供的功能并不多,不能满...
  • Kaitiren
  • Kaitiren
  • 2015年09月12日 18:18
  • 10221

hdu 5451 Best Solver 快速矩阵乘法 Fibonacci数列的循环节

Best Solver Time Limit: 1500/1000 MS (Java/Others)    Memory Limit: 65535/102400 K (Java/Others) Tot...
  • firenet1
  • firenet1
  • 2015年09月22日 21:54
  • 1085

“Docker Practice”学习笔记

一本很不错的Docker入门书籍,github地址, 自己仔细看完了前14章, 提了一个pull request, 修改了3个issue! 关键字:docker, Union FS(AUFS), c...
  • wuzhimang
  • wuzhimang
  • 2017年06月18日 15:06
  • 525
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:(转贴)Struts best practices 4
举报原因:
原因补充:

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