【正文】
ional 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。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。 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。. 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(()))。 圖 1. Struts 和 MVC Struts 組件概覽 首 先,我們在最佳實踐上下文中解釋 Struts 組件,以及它們在 Web 應(yīng)用程序開發(fā)中所起的作用。 ActionForm 作為運載工具,向 Action 類提供請求數(shù)據(jù)。 ActionErrors 可以使用 ActionError 來支持異常處理。 清單 1. 使用 ActionForm html:form action=/bp1 html:text property=attrib1 / /html:form 這個 ActionForm 被稱為 “ BP1AForm” ,它包括屬性 attrib1 及其 getter 和