【文章內(nèi)容簡介】
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()。 ActionError actionError = new ActionError(())。 (, actionError)。 saveErrors(request, actionErrors)。 } While conventional exception handling procedures save exception information in every Action class, best practice 4 aims to avoid redundant code while handling exceptions. To use this practice, Struts remends following these steps: 1. Create an Action class, say BP4Action, by extending . 2. Create all other Action classes in your Web application by extending BP4Action. 3. In BP4Action, declare variable ActionErrors actionErrors = new ActionErrors()。. 4. In BP4Action, create a method performTask() as in public abstract ActionForward performTask(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response, ActionErrors actionErrors) throws IOException, ServletException. 5. In BP4Action, declare method perform() as final. Then invoke generic methods, which must always be called before processing the request. Now you can call the method performTask() created in the previous step. 6. While implementing method performTask() in every Action class (by extending BP4Action), handle application exceptions as shown in Listing 4. Listing 4. Using ActionErrors effectively try { //Code in Action class } catch(ApplicationException appException) { //Log exception //Add error to actionErrors (, new ActionError(()))。 } In BP4Action, after invoking the method performTask(), save the ActionErrors using saveErrors(request, errors). Advantages This practice39。s main advantage is that it avoids code redundancy in every Action class that handles ActionErrors. In conclusion Building an easily maintainable Web application can be one of the most challenging tasks for a development team. Using a mature framework like Struts helps you implement the infrastructure code normally associated with building an application. The St