【正文】
perform() 方法提供其特定于用例的實(shí)現(xiàn)。 JSP 定制標(biāo)記庫 JSP 定制標(biāo)記庫是用標(biāo)記表示的一組行為的集合。 ActionError 可以封裝錯(cuò)誤消息,而 Presentation 層中的 /html:errors 可以呈現(xiàn) ActionError 集合內(nèi)的所有錯(cuò)誤消息。 要實(shí)現(xiàn)這一最佳實(shí)踐, Struts 建議您進(jìn)行以下兩個(gè)操作: 1. 創(chuàng)建一個(gè) JavaBean( BP1BForm),且其屬性是 BP1AForm 屬性的子集,還要?jiǎng)?chuàng)建這些屬性的 getter 和 setter 方法。 3. 在 BP2Action 類中創(chuàng)建一個(gè)方法 performTask(),就像在公共抽象類 ActionForward performTask(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException 中一樣。 優(yōu)勢 這一實(shí)踐有兩個(gè)主要優(yōu)勢。因 此,它實(shí)現(xiàn)了在 Web 應(yīng)用程序中保持會(huì)話數(shù)據(jù)。然后此類創(chuàng)建一個(gè) ActionError 并在合適的作用域中存儲(chǔ)它。 要使用這一最佳實(shí)踐, Struts 建議您遵循以下步驟: 1. 通過擴(kuò)展 創(chuàng)建一個(gè) Action 類,比如 BP4Action。 清單 4. 有效使用 ActionErrors try { //Code in Action class } catch(ApplicationException appException) { //Log exception //Add error to actionErrors (, new ActionError(()))。它們不僅能夠提高代碼的模塊化程度和應(yīng)用程序的可重用性,還能減少代碼冗余。使用 Struts 等成熟的框架有助于實(shí)現(xiàn)通常與構(gòu)建應(yīng)用程序相關(guān)的基礎(chǔ)設(shè)施代碼。 5. 在 BP4Action 中將方法 perform() 聲明為 final。 ActionError actionError = new ActionError(())。 4. 相應(yīng)的 Action 類可以決定這些方法的調(diào)用順序。這些 ActionForm 封裝頁面數(shù)據(jù),并提供一個(gè)驗(yàn)證框架來驗(yàn)證請求參數(shù)。 5. 在 BP2Action 類中,將方法 perform() 聲明為 final。 清單 2. 訪問 JSP 中的表單屬性 html:form action=/bp1 bean:define name=bp1AForm property=bp1BForm id=bp1B type= / html:text name=bp1B property=subsetAtt1 / /html:form 最佳實(shí)踐 2. 使用 Action 類處理請求 通常,在使用這個(gè) Struts 框架時(shí),對于 JSP 組件請求應(yīng)用程序執(zhí)行的每個(gè)動(dòng)作,應(yīng)用程序都必須擴(kuò)展 Struts 的 以創(chuàng)建 Action 類。清單 1 展示了 ActionForm 標(biāo)記在 View 層中的傳統(tǒng)方法。 Struts 提供的 JSP 標(biāo)記包括 HTML、邏輯和 bean 標(biāo)記。要驗(yàn)證請求數(shù)據(jù), ActionForm 的 validate() 方法必須給出一個(gè)特定于該情況的實(shí)現(xiàn)。圖 1 展示了基于 ModelViewController 設(shè)計(jì)的 Struts 框架。 } While conventional exception handling procedures save exception information in every Action class, best practice 4 aims to avoid redundant code while handling exceptions. To use this practice, Struts remends following these steps: 1. Create an Action class, say BP4Action, by extending . 2. Create all other Action classes in your Web application by extending BP4Action. 3. In BP4Action, declare variable ActionErrors actionErrors = new ActionErrors()。 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 convent