【正文】
Best practices for Struts development Palaniyappan Thiagarajan, Pagadala Suresh Struts: A brief introduction Struts, an open source framework you can use to build Web applications, is based on the popular ModelViewController (MVC2) 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。 . These Action classes provide an interface to the application39。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。 . ActionForms are simple JavaBeans that encapsulate and validate request parameters. To validate your request data, your ActionForm39。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。s View layer, where almost every form field of the JSP object maps to an attribute of the corresponding ActionForm. JSP custom tag libraries The JSP custom tag libraries are a collection of actions presented as tags. This is a powerful feature of the JSP Specification 。 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 in