【正文】
) design paradigm. The framework is built upon standard technologies like Java Servlets, JavaBeans, ResourceBundles, and XML, and it provides flexible and extensible ponents. Struts implements the Controller layer in the form of ActionServlet and remends building the View layer using JSP tag libraries. Struts also provides a wrapper around the Model layer throughAction classes. Figure 1 illustrates the Struts framework based on the ModelViewController design. Figure 1. Struts and MVC Overview of Struts ponents First, we39。ll explain the Struts ponents in the context of best practices and the role each one plays in your Web application development. Action Every Action of your application extends Struts39。s Model layer, acting as a wrapper around the business logic. Each Action class must provide its casespecific implementation to the perform() method. The perform() method always returns a value of type ActionForward. ActionForm Every ActionForm of your application extends Struts39。s validate() method must give a casespecific implementation. ActionForms serve as a carrier of request data to the Action class. A JSP object bines with a respective ActionForm to form your application39。 it allows you to separate presentation from other application tiers. The libraries are easy to use and you can read them in XMLlike fashion. You can easily maintain the JSP ponents by minimizing the use of Java scriptlets in them. The JSP tags that Struts provides include HTML, logic, and bean tags. ActionErrors You use ActionErrors to support exception handling. An ActionError traps and propagates an application exception to the View layer. Each one is a collection of ActionError instances. ActionErrors encapsulate error messages, while the /html:errors in the Presentation layer renders all error messages in the ActionError collection. Best Practice 1. Reuse data across multiple ActionForms Now that you are familiar with the Struts ponents, we will continue by showing you ways to get the most out of the framework. First, Struts remends that you associate every JSP object with an ActionForm, which encapsulates data represented in the screen. You access the form data in the JSP object using accessory methods found in ActionForm. Listing 1 shows the conventional use of ActionForm tag in the View layer. Listing 1. Using ActionForm in JSP html:form action=/bp1 html:text property=attrib1 / /html:form The ActionForm called BP1AForm includes 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。 to create an Action class. This individual Action class interfaces with the application39。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