|
||||||||||||
|
|
| Take Command of Your Software (3) |
| 时间:22/04/2007 作者:网络 来源:网络 |
| 小提示→点这里把文章加入您的收藏夹,方便下次查看 |
| 设置文章字体大小:[大 中 小] |
Example 2. WEB-INF/web.xml
The preceding deployment descriptor does three things:
Example 3 lists test.jsp, the JSP shown in Figure 6's top picture: Example 3. test.jsp
The preceding JSP creates an HTML anchor element that references the /simple.do URL. If you click on that link, the Struts action servlet is invoked because the mapping in the deployment descriptor maps all URLs that end in .do to the action servlet (see Example 2). So what does the action servlet do when it receives the /simple.do URL? The answer resides in the Struts configuration file, which is specified in the deployment descriptor (see Example 2) and listed in Example 4: Example 4. WEB-INF/action.xml
The preceding configuration file maps the /simple URL to the actions.SimpleAction class. When the action servlet receives the /simple.do URL, it strips off the .do suffix and maps the URL to the actions.SimpleAction class. If an instance of that class does not exist, the action servlet creates one and invokes its execute() method. Example 5 lists the actions.SimpleAction class: Example 5. WEB-INF/classes/actions/SimpleAction.java
The preceding action checks to see if a counter bean exists in the application scope; if not, it creates one and stores it there. Subsequently, the action invokes the counter bean's updateCount() method, which updates a counter stored in a file named .simpleActionCount. Then the action returns an ActionForward instance that points to a JSP mapped to the string fwd-page. That mapping, also defined in the Struts configuration file (see Example 4), resolves the string fwd-page to the forwardPage.jsp JSP. The action servlet subsequently forwards control to that JSP, listed in Example 6: Example 6. forwardPage.jsp
The preceding JSP uses the JSTL <c:out>, <c:choose>, <c:when>, and <c:otherwise> actions to display information about how many times the counter bean, and therefore the simple action, has been accessed. JSTL defines an expression language that will be incorporated into the upcoming JSP 2.0. The preceding JSP, for example, uses that expression language to access the counter bean in application scope. (You can download JSTL from Resources.) Finally, Example 7 lists the counter bean class—beans.CounterBean: Example 7. WEB-INF/classes/beans/CounterBean.java
The CounterBean class updates the count associated with a particular action. That class serves as a receiver, as I discussed at this article's beginning, by implementing application-specific behavior. Your wish is my commandUnless you develop application frameworks, you may never need to implement the Command pattern; nevertheless, you should understand how it works so you can effectively use application frameworks such as Swing and Struts. Now that you've seen how to use the Command pattern in Swing and Struts, you'll be able to better use those and other OO frameworks you encounter in the future. HomeworkFor your homework assignment, download Struts, then add your own custom action to the example discussed above. That custom action can implement whatever application-specific behavior you desire. Homework from last timeIn your homework assignment from "Strategy for Success" (JavaWorld, April 2002), I asked you to discuss how Swing uses the Strategy pattern in its list class to render list cells. As "Strategy for Success" outlined, the Swing JComponent class uses the Strategy pattern to paint borders around components. The JComponent class also uses the Strategy pattern to paint Swing components themselves by delegating that behavior to individual Swing components; for example, the JComponent.paint() method invokes the paintComponent() method, which JComponent subclasses override to paint the component. Some Swing components, those with more complicated rendering needs, do not paint themselves; instead, they use the Strategy pattern to delegate that behavior to another object. For example, Swing lists delegate the painting of their list cells to an object that implements the ListCellRenderer interface. That interface defines a single method—getListCellRendererComponent()—that returns a component that paints the list's cells. That Strategy pattern usage lets developers modify how lists paint their cells by implementing a list cell renderer and attaching it to a specific list. |
|
上一篇:Take Command of Your Software (1)
下一篇:爪哇语言抽象工厂创立性模式介绍 |
| 【返回】 【顶部】 【关闭】 |
| Copyright © 2005-2010 www.594k.com All Rights Reserved. |
| 版权所有:JAVA学习网
备案序号:皖ICP备06004238号 |