【正文】
gging business logic into the application, a consistent mechanism across development teams for performing tasks such as user data validation, screen navigation, and so forth, as well as a set of custom tag libraries to simplify developing screens. These four best practices are important for you to extract more from the framework39。 saveErrors(request, actionErrors)。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 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 。 . These Action classes provide an interface to the application39。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 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。 to create an Action class. This individual Action class interfaces with the application39。 (, actionError)。 } In BP4Action, after invoking the method performTask(), save the ActionErrors using saveErrors(request, errors). Advantages This practice39。 Struts 以 ActionServlet 的形式實現(xiàn)了 Controller 層,并建議使用 JSP 標記庫構(gòu)建 View 層。 Action 應(yīng)用程序的每個 Action 都會擴展 Struts 的 類。 ActionForm 應(yīng)用程序的 ActionForm 擴展了 Struts 的 類。一個 JSP 對象與各自的 ActionForm 對象相結(jié)合,構(gòu)成應(yīng)用程序的 View 層。這些庫易于使用,而且可以以一種類似 XML 的方式來讀取。 ActionError 捕捉應(yīng)用程序異常,并將其傳送給 View 層。首先, Struts 建議將每個 JSP 對象與一個 ActionForm 相關(guān)聯(lián),后者可以封裝屏幕上 顯示的數(shù)據(jù)。在配置文件 中,行為 “ /bp1” 通過 name 屬性映射到 bp1AForm?,F(xiàn)在就可以通過 BP1BForm 訪問 BP1AForm 中的屬性子集了。 要實現(xiàn)這一最佳實踐, Struts 建議您遵循以下步驟: 1. 通過擴展 創(chuàng)建一個 Action 類,比如 BP2Action??紤]以下因素后決定方法的訪問修飾符: o 如果所有 Action 類都必須實現(xiàn)此方法,則讓其為抽象。現(xiàn)在調(diào)用 步驟 3 中創(chuàng)建的方法 performTask()。其次,通過將 Action 類的行為集中在一起,使應(yīng)用程序能夠更多地控制通用的任務(wù)。這種最佳實踐實現(xiàn)了這種 Web 應(yīng)用程序特性。 2. 在 BP3Form 類中,添加具有訪問修飾語的方法,就像在公共抽象類 void toSession(SessionData sessionData) 和 void fromSession(SessionData sessionData) 中一樣。 何時使用這一實踐 這一實踐最適用于:會話數(shù)據(jù)是單一對象和 /或每個頁操作或使用會話數(shù)據(jù)。清單 3 展示了 Action 類是如何處理異常的。 saveErrors(request, actionErrors)。 3. 在 BP4Action 中聲明變量 ActionErrors actionErrors = new ActionErrors()。現(xiàn)在調(diào)用在前一個步驟中創(chuàng)建的 performTask()。 優(yōu)勢 這一實踐主要的優(yōu)勢是:避免了每個處理 ActionErrors 的 Action 類中的代碼冗余 。此外,還提供了一種跨開發(fā)團隊的一致機制,用于執(zhí)行用戶數(shù)據(jù)驗證、屏幕導(dǎo)航等任務(wù),以及用于簡化開發(fā)屏幕的一組定制標記庫。 本文譯自: developerWorks Web development Technical library