【正文】
cludes the attribute attrib1, as well as its getter and setter methods. In the configuration , the action /bp1 maps to bp1AForm using the name attribute. This facilitates data display in the JSP. To implement this best practice, Struts remends you do two things: 1. Create a JavaBean (BP1BForm) with attributes that form an attribute subset in BP1AForm, along with the attributes39。 getter and setter methods. 2. Replace the attributes in BP1AForm with the bean BP1BForm by associating the bean with BP1AForm. Now you can access this attribute subset in BP1AForm through BP1BForm. Listing 2 shows you how. Listing 2. Accessing form attributes in JSP html:form action=/bp1 bean:define name=bp1AForm property=bp1BForm id=bp1B type= / html:text name=bp1B property=subsetAtt1 / /html:form Best Practice 2. Use Action class to handle requests Typically when using the Struts framework, for every action the JSP ponent requests your application to execute, the application must extend Struts39。 to create an Action class. This individual Action class interfaces with the application39。s Model layer while processing the request. To implement this practice, Struts remends you follow these steps: 1. Create an Action class, say BP2Action, by extending . 2. Create all other Action classes in your Web application by extending BP2Action. 3. In BP2Action, create a method performTask(), as in public abstract ActionForward performTask(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException. 4. In BP2Action add one or more generic methods to the application, for example serverSideValidate(). You can decide on the method39。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