(转贴)Struts best practices 3

转载 2004年09月13日 15:56:00
Page 3 of 4

Prepopulation
Problem
One requirement difficult to achieve is data prepopulation in a drop-down list (HTML Select tag) or in other fields. Some of these values come from a database, some from the application's context, and the rest are passed from calling screens. These values must be populated in a form bean before forwarding the control to the JSP page.

Struts best practice
The form bean's prepopulation can be accomplished using one of the following approaches:

  • The on-demand lazy-loading technique for all such data in the application's context, which is required for prepopulation. An application screen's (JSP page) invocation mechanism can always be routed though a specific method of corresponding Action classes. Prepopulation-related code can be placed in this method.

  • Have different Action classes—one for requesting and another for submitting the JSP pages. The entire module or application can share the Action class used for requesting.

The second approach works well for small applications. However, for large applications, managing numerous Action classes grows cumbersome. Therefore, the first approach should be adopted.

Stack maintenance (for bread crumbs)
Problem
What if you need to go to a JSP page from two or more different JSP pages and subsequently return to the calling JSP page? Similarly, often developers need to go to various JSP pages in a criss-cross manner, which grows more complex when breadcrumbs need to be shown. Thus, the application must remember the pages visited.

Struts best practice
The remedy is to maintain the paths of all the forwarded JSP pages in a Stack (java.util.Stack). The following steps are required for maintaining such a Stack:

  1. Extend the RequestProcessor class and override the method processActionPerform(). In the overridden method, after the call to super.processActionPerform(), ActionForward's path should be stored in the Stack.

  2. In the application's parent Action class, provide methods to traverse forward and backward in the Stack.

Another option is to utilize an already available open source project at sourceforge.net like Open Tranquera (see Resources for more details).

Context-related issues
Problem
If ActionForward's path and the path to which the control is forwarded differ, a context problem has resulted. More such context-related problems are quite frequent in Struts-based applications, most of which can be solved using some of Struts' less exploited features.

Struts best practice
Mention the JSP page's name as a path for ActionForward and let the prefix—i.e., the JSP page's access path—come from a preconfigured place. Do that in the following manner:

struts-config.xml(s) has a controller tag; add a property in this tag named forwardPattern. This property's value can be prefixed to all the ActionForward paths. This property's default value is $M$P, which means the module prefix will be prefixed to ActionForward's path. We can change this value to anything; e.g., WEB-INF/pages/$M$P. As a result, all the ActionForward paths will be searched in the directory WEB-INF/pages//.

For above solution to work, make sure that ActionForward's contextRelative property is marked false. If this property is marked true, the ActionForward paths will be taken since they are not modified.

Also note that for forwardPattern to work, the path of all ActionForwards must start with a /.

Form-bean scope
Problem
A well-managed session context can greatly boost application performance. It is therefore important to understand the implications of marking the form-bean scope to request, session, or application. However, the following confusions usually arise:

  • If the scope of the form beans is marked request, how will the information available in one JSP page/screen also be available to another one?

  • If the scope is session, how will the server know that the client is no longer interested in that form bean and consequently destroy it?

Struts best practice
State can be maintained in HttpSession (Web tier), in stateful session beans (EJB tier), or in a database. The appropriate choice should be based on what kind availability needs persist or whether the application is supposed to provide multichannel access. However, the rule of thumb is to maintain the state close to the tier that requires it most. Therefore, the best practice is to keep the state in HttpSession; i.e., mark the form-bean scope to session.

The important question is when and how to destroy the form beans stored in the HttpSession. The session beans should be destroyed in an extended RequestProcessor based on the least-recently-accessed, to-be-removed logic. Another approach is to destroy all the form beans as soon as the user invokes a new business operation, possibly from the application menu.

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

Page 4 of 4 Data transfer object implementation Problem Usually a data transfer object (DTO) is used...
  • tele
  • tele
  • 2004年09月13日 15:58
  • 886

(转贴)Struts best practices 1

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

<<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
  • 2075

最佳实践(Best Practices)

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

C++ Best Practice

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

svn分支合并

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

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
  • 1003

The Six Best Practices(1~3)

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

JUnit best practices

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

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