【正文】
如果 你在一個(gè)團(tuán)隊(duì)環(huán)境中工作,不同的團(tuán)隊(duì)可能有不同的 ActionMapping 名稱空間使用。 ActionMapping自身是一個(gè) “平面的 ” 名稱空間,完全沒(méi)有一個(gè)內(nèi)部層次關(guān)系。但實(shí)際上它并 不代表一個(gè)文件。 19 unknown 如果該 mapping 要被配置為應(yīng)用的缺省 mapping(處理那些沒(méi)有被其它 mapping處理的 請(qǐng)求 ),可設(shè)置為 true。 . attribute 請(qǐng)求 范圍或者會(huì)話 范圍的屬性名稱, form bean 在下面被訪問(wèn)。而是在 form bean 配置中 使用的邏輯名稱。 實(shí)際上是 forward, include, type 屬性的一個(gè),必須標(biāo)明。 屬性 說(shuō)明 path 來(lái)自于請(qǐng)求的 URI路徑,用來(lái)選擇該 mapping。 當(dāng)前,我們引用 ActionMapping 類,但你應(yīng)該注意到在 Struts ,所有的 action 屬性實(shí)際上 都在 ActionConfig超類中有定義。 Mapping處于每個(gè) Struts 應(yīng)用的絕對(duì)核心。取決于 mapping 如何被填充,可 能被傳遞到上述的任何地方。它也許需要將控制轉(zhuǎn)發(fā)到其他資源。更多信息,參見(jiàn)第 4章。通 過(guò)這種方式, ActionMapping 以自己的方式發(fā)展為一個(gè)對(duì)象,可以和 Action一起使用,也 可以不。一系列 ActionMapping 對(duì)象被放在一個(gè) ActionMappings 集合之中 ()。 所以,底線是什么?為以一種靈活和有效的方式實(shí)現(xiàn) Model 2,我們需要 ? 路由對(duì)業(yè)務(wù)操作的請(qǐng)求到一個(gè) servlet ? 決定哪個(gè)業(yè)務(wù)操作和請(qǐng)求相關(guān) 16 ? 裝入多線程的 helper 對(duì)象來(lái)處理業(yè)務(wù)操作 ? 將每個(gè)請(qǐng)求特定的配置細(xì)節(jié)傳遞給 helper對(duì)象。如何組織 URI 來(lái)觸 發(fā)業(yè)務(wù)操作是開(kāi)發(fā) web 應(yīng)用的關(guān)鍵 部分。 Struts 通過(guò)使 ActionServlet 代表其他對(duì)象的業(yè)務(wù)操作來(lái)做這些事情。 Servlet設(shè)計(jì)來(lái)可以處理大 量的并行請(qǐng)求。這可以是一個(gè)常用的模板格式,象 *.do, 或者特別的路徑 , 象 。 This does not affect how the properties are written by the tag extensions. It affects how the autopopulation mechanism perceives them in the request. Nested ponents 203 The unknown ActionMapping While surfing the Web, most of us have encountered the dreaded 404— page not found message. Most web servers provide some special features for processing requests for unknown pages, so webmasters can steer users in the right direction. Struts offers a similar service for ActionMapping 404s—the unknown ActionMapping. In the Struts configuration file, you can specify one ActionMapping to receive any requests for an ActionMapping that would not otherwise be matched: action name=/debug forward=/pages// When this option is not set, a request for an ActionMapping that cannot be matched throws 400 Invalid path /notHere was requested Note that by a request for an ActionMapping, we mean a URI that matches the prefix or suffix specified for the servlet (usually /do/* or *.do). Requests for other URI patterns, good or bad, will be handled by other servlets or by the container: 12 /do/notHere (goes to the unknown ActionMapping) / (goes to the container) Nested ponents The ActionMapping properties are helpful when it es to getting an Action to run a business operation. But they tell only part of the story. There is still much to do when the Action returns. An Action may have more than one oute. We may need to register several ActionForwards so that the Action can take its pick. Local forwards In the normal course, an ActionMapping is used to select an Action object to handle the request. The Action returns an ActionForward that indicates which page should plete the response. The reason we use ActionForwards is that, in practice, presentation pages are either often reused or often changed, or both. In either case, it is good practice to encapsulate the page’s location behind a logical name, like “success” or “failure.” The ActionForward object lets us assign a logical name to any given URI. 204 CHAPTER 7 Designing with ActionMappings Of course, logical concepts like success or failure are often relative. What represents success to one Action may represent failure to another. Each Action Mapping can have its own set of local ActionForwards. When the Action asks for a forward (by name), the local set is checked before trying the global forwards. See chapter 6 for more about ActionForwards. Local forwards are usually specified in the Struts configuration file. See chapter 4 for details. Local exceptions Most often, an application’s exception handlers (. ExceptionHandler) can be declared globally. However, if a given ActionMapping needs to handle an exception differently, it can have its own set of local exception handlers that are checked before the global set. Local exceptions are usually specified in the Struts configuration file. See chapter 4 for details. 13 Rolling your own ActionMapping While ActionMapping provides an impressive array of properties, developers may also provide their own subclass with additional properties or methods. In Struts , this is configured in the deployment descriptor () for the ActionServlet: initparam paramnamemapping/paramname paramvalue/paramvalue /initparam In Struts , this is configured in the Struts configuration file as an attribute to the actionmappings element: actionmappings type= Individual mappings may also be set to use another type through the className attribute: action className= For more about configuring Struts, see chapter 4. Since Struts Summary 205 The framework provides two base ActionMapping classes, shown in table . They can be selected as the default or used as a base for your own subclasses. The framework default is SessionActionMapping, so scope defaults to session. Subclasses that provide new properties may set them in the Struts configuration using a standard mechanism: setproperty property=myProperty value=myValue / Using this standard mechanism helps developers avoid subclassing the Action Servlet just to recognize the new properties when it digests the configuration file. This is actually a feature of the Digester that Struts simply inherits. Summary Sun’s Model 2 architecture teaches that servlets and JavaServer Pages should be used together in the same application. The servlets can handle flow control and data acquisition, and the JavaServer Pages can handle the HTML. 14 Struts takes this one step further and delegates much of the flow control and data acquisition to Action objects. The application then needs only a single servlet to act as a traffic cop. All the real work is parceled out to the Actions and the Struts configuration objects. Like servlets, Actions are efficient, multithreaded singletons. A single Action object can be handling any number o