【正文】
ward. ActionForm Every ActionForm of your application extends Struts39。s access modifier by considering the following factors: o If all Action classes must implement this method, make it abstract. o If some Action classes will provide a casespecific implementation, declare the method protected and give it a default implementation. 5. In BP2Action, declare method perform() as final. Invoke the above generic method, which must always be called before processing the request. Now call the method performTask() created in step 3. 6. In every Action class extending BP2Action, add method performTask() with a casespecific implementation. Advantages This practice has two main advantages. First, it helps you avoid redundant code in every Action class of your Web application. Second, it gives the application more control over generic tasks by centralizing the behavior in one Action class. Best Practice 3. Use ActionForm to work on session data In a Strutsbased Web application, each ActionForm extends . These ActionForms encapsulate page data and provide a validation framework to validate request parameters. Most Web applications maintain data in session to make them available throughout the application. This best practice addresses this Web application feature. It allows methods toSession() and fromSession() to move session data to and from the form data. Thus, it addresses session data maintenance in a Web application. To adhere to this practice, follow these steps: 1. Create an abstract class named BP3Form by extending . 2. In BP3Form, add methods with access modifiers as in public abstract void toSession(SessionData sessionData) and void fromSession(SessionData sessionData). 3. In every ActionForm, extend BP3Form and implement the abstract methods in which the form data is transported to and from the session. 4. The corresponding Action class may determine the order in which these methods are called. For example, you could invoke method toSession() on the ActionForm just before actionForward is determined. When to use this practice This practice is most useful when session data is maintained as a single object and/or every page manipulates or uses session data. Best Practice 4. Handle exceptions effectively Conventionally, when an application exception occurs in an Action class, the exception is first logged. Then the class creates anActionError and stores it in the appropriate scope. This Action class then forwards control to the appropriate ActionForward. Listing 3 shows how Action class handles exceptions. Listing 3. Exception handling in an Action class try { //Code in Action class } catch (ApplicationException e) { //log exception ActionErrors actionErrors = new ActionErrors()。s features. You, as a developer, can benefit from these lessons to increase your code modularity and application reusability, plus minimize code redundancy. These are all critical to building an extensible Web application. 譯文 Struts 開(kāi)發(fā)的最佳實(shí)踐 Palaniyappan Thiagarajan, Pagadala Suresh Struts:簡(jiǎn)介 Struts 是一種開(kāi)源框架,可用來(lái)構(gòu)建 Web 應(yīng)用程序,它基于流行的 ModelViewController (MVC2) 設(shè)計(jì)范型。每個(gè) Action 類(lèi)都必須向