📄 jsptags7.html
字号:
</table> </body></html><a name="wp90684"> </a></pre></div><a name="wp90685"> </a><h5 class="pHeading4">Tag Handler</h5><a name="wp90686"> </a><p class="pBody">The tag handler passes the current element of the group back to the page in an EL variable called <code class="cCode">var</code>, which is accessed using the expression language in the calling page. After the variable is set, the body is evaluated with the <code class="cCode">invoke</code> method.</p><div class="pPreformattedRelative"><pre class="pPreformattedRelative">public void doTag() throws JspException, IOException { if (iterator == null) return; while (iterator.hasNext()) { getJspContext().setAttribute(var, iterator.next()); getJspBody().invoke(null); }}public void setVar(String var) { this.var = var;}public void setGroup(Collection group) { this.group = group; if(group.size() > 0) iterator = group.iterator();}<a name="wp90687"> </a></pre></div><a name="wp90689"> </a><h4 class="pHeading3">A Template Tag Library</h4><a name="wp90690"> </a><p class="pBody">A template provides a way to separate the common elements that are part of each screen from the elements that change with each screen of an application. Putting all the common elements together into one file makes it easier to maintain and enforce a consistent look and feel in all the screens. It also makes development of individual screens easier because the designer can focus on portions of a screen that are specific to that screen while the template takes care of the common portions.</p><a name="wp90691"> </a><p class="pBody">The template is a JSP page with placeholders for the parts that need to change with each screen. Each of these placeholders is referred to as a <em class="cEmphasis">parameter</em> of the template. For example, a simple template could include a title parameter for the top of the generated screen and a body parameter to refer to a JSP page for the custom content of the screen.</p><a name="wp90693"> </a><p class="pBody">The template uses a set of nested tags--<code class="cCode">definition</code>, <code class="cCode">screen</code>, and <code class="cCode">parameter</code>--to define a table of screen definitions and uses an <code class="cCode">insert</code> tag to insert parameters from a screen definition into a specific application screen.</p><a name="wp90695"> </a><h5 class="pHeading4">JSP Pages</h5><a name="wp90697"> </a><p class="pBody">The template for the Duke's Bookstore example, <code class="cCode"><a href="../examples/web/bookstore3/web/template/template.txt" target="_blank">template.jsp</a></code>, is shown below. This page includes a JSP page that creates the screen definition and then uses the <code class="cCode">insert</code> tag to insert parameters from the definition into the application screen.</p><div class="pPreformattedRelative"><pre class="pPreformattedRelative"><%@ taglib uri="/tutorial-template" prefix="tt" %><%@ page errorPage="/template/errorinclude.jsp" %><%@ include file="/template/screendefinitions.jsp" %><html><head><title><tt:insert definition="bookstore" parameter="title"/></title></head><body bgcolor="#FFFFFF"> <tt:insert definition="bookstore" parameter="banner"/><tt:insert definition="bookstore" parameter="body"/><center><em>Copyright &copy; 2002 Sun Microsystems, Inc. </em></center></body></html><a name="wp90698"> </a></pre></div><a name="wp90700"> </a><p class="pBody"><code class="cCode"><a href="../examples/web/bookstore3/web/template/screendefinitions.txt" target="_blank">screendefinitions.jsp</a></code> creates a screen definition based on a request attribute <code class="cCode">selectedScreen</code>:</p><div class="pPreformattedRelative"><pre class="pPreformattedRelative"><tt:definition name="bookstore"screen="${requestScope ['javax.servlet.forward.servlet_path']}"> <tt:screen id="/bookstore"> <tt:parameter name="title" value="Duke's Bookstore" direct="true"/> <tt:parameter name="banner" value="/template/banner.jsp" direct="false"/> <tt:parameter name="body" value="/bookstore.jsp" direct="false"/> </tt:screen> <tt:screen id="/bookcatalog"> <tt:parameter name="title" direct="true"> <jsp:attribute name="value" > <fmt:message key="TitleBookCatalog"/> </jsp:attribute> </tt:parameter> <tt:parameter name="banner" value="/template/banner.jsp" direct="false"/> <tt:parameter name="body" value="/bookcatalog.jsp" direct="false"/> </tt:screen> ...</tt:definition><a name="wp90701"> </a></pre></div><a name="wp90703"> </a><p class="pBody">The template is instantiated by the <code class="cCode"><a href="../examples/web/bookstore3/src/Dispatcher.java" target="_blank">Dispatcher</a></code> servlet. <code class="cCode">Dispatcher</code> first gets the requested screen and stores it as an attribute of the request. This is necessary because when the request is forwarded to <code class="cCode">template.jsp</code>, the request URL doesn't contain the original request (for example, <code class="cCode">/bookstore3/catalog</code>) but instead reflects the path (<code class="cCode">/bookstore3/template.jsp</code>) of the forwarded page. Then <code class="cCode">Dispatcher</code> performs business logic based on the request URL, which updates model objects. Finally, the servlet dispatches the request to <code class="cCode">template.jsp</code>:</p><div class="pPreformattedRelative"><pre class="pPreformattedRelative">public class Dispatcher extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) { String bookId = null; BookDetails book = null; String clear = null; BookDBAO bookDBAO = (BookDBAO)getServletContext(). getAttribute("bookDBAO"); HttpSession session = request.getSession(); String selectedScreen = request.getServletPath(); ShoppingCart cart = (ShoppingCart)session. getAttribute("cart"); if (cart == null) { cart = new ShoppingCart(); session.setAttribute("cart", cart); } request.setAttribute("selectedScreen", request.getServletPath()); if (selectedScreen.equals("/bookcatalog")) { bookId = request.getParameter("Add"); if (!bookId.equals("")) { try { book = bookDBAO.getBookDetails(bookId); if ( book.getOnSale() ) { double sale = book.getPrice() * .85; Float salePrice = new Float(sale); book.setPrice(salePrice.floatValue()); } cart.add(bookId, book); } catch (BookNotFoundException ex) { // not possible } } } else if (selectedScreen.equals("/bookshowcart")) { bookId =request.getParameter("Remove"); if (bookId != null) { cart.remove(bookId); } clear = request.getParameter("Clear"); if (clear != null && clear.equals("clear")) { cart.clear(); } } else if (selectedScreen.equals("/bookreceipt")) { // Update the inventory try { bookDBAO.buyBooks(cart); } catch (OrderException ex) { request.setAttribute("selectedScreen", "/bookOrderError"); } } try { request. getRequestDispatcher( "/template/template.jsp"). forward(request, response); } catch(Exception ex) { ex.printStackTrace(); } } public void doPost(HttpServletRequest request, HttpServletResponse response) { request.setAttribute("selectedScreen", request.getServletPath()); try { request. getRequestDispatcher( "/template/template.jsp"). forward(request, response); } catch(Exception ex) { ex.printStackTrace(); } }}<a name="wp90704"> </a></pre></div><a name="wp90705"> </a><h5 class="pHeading4">Tag Handlers</h5><a name="wp90706"> </a><p class="pBody">The template tag library contains four tag handlers--<code class="cCode">DefinitionTag</code>, <code class="cCode">ScreenTag</code>, <code class="cCode">ParameterTag</code>, and <code class="cCode">InsertTag</code>--that demonstrate the use of cooperating tags. <code class="cCode">DefinitionTag</code>, <code class="cCode">ScreenTag</code>, and <code class="cCode">ParameterTag</code> comprise a set of nested tag handlers that share private objects. <code class="cCode">DefinitionTag</code> creates a public object named <code class="cCode">bookstore</code> that is used by <code class="cCode">InsertTag</code>.</p><a name="wp90708"> </a><p class="pBody">In <code class="cCode">doTag</code>, <code class="cCode"><a href="../examples/web/bookstore3/src/template/DefinitionTag.java" target="_blank">DefinitionTag</a></code> creates a private object named <code class="cCode">screens</code> that contains a hash table of screen definitions. A screen definition consists of a screen identifier and a set of parameters associated with the screen. These parameters are loaded when the body of the definition tag, which contains nested <code class="cCode">screen</code> and <code class="cCode">parameter</code> tags, is invoked. <code class="cCode">DefinitionTag</code> creates a public object of class <code class="cCode"><a href="../examples/web/bookstore3/src/template/Definition.java" target="_blank">Definition</a></code>, selects a screen definition from the <code class="cCode">screens</code> object based on the URL passed in the request, and uses it to initialize a public <code class="cCode">Definition</code> object.</p><div class="pPreformattedRelative"><pre class="pPreformattedRelative">public int doTag() { try { screens = new HashMap(); getJspBody().invoke(null); Definition definition = new Definition(); PageContext context = (PageContext)getJspContext(); ArrayList params = (ArrayList) screens.get(screenId); Iterator ir = null; if (params != null) { ir = params.iterator(); while (ir.hasNext()) definition.setParam((Parameter)ir.next()); // put the definition in the page context context.setAttribute(definitionName, definition, context.APPLICATION_SCOPE); } }<a name="wp90710"> </a></pre></div><a name="wp90711"> </a><p class="pBody">The table of screen definitions is filled in by <code class="cCode">ScreenTag</code> and <code class="cCode">ParameterTag</code> from text provided as attributes to these tags. <a href="JSPTags7.html#wp90722">Table 15-14</a> shows the contents of the screen definitions hash table for the Duke's Bookstore application</p><div align="left"><table border="1" summary="Screen Definitions" id="wp90722"> <caption><a name="wp90722"> </a><div class="pTableTitle">Table 15-14 Screen Definitions </div></caption> <tr align="center"> <th><a name="wp90730"> </a><div class="pCellHeading">Screen Id</div></th> <th><a name="wp90732"> </a><div class="pCellHeading">Title</div></th> <th><a name="wp90734"> </a><div class="pCellHeading">Banner</div></th> <th><a name="wp90736"> </a><div class="pCellHeading">Body</div></th></tr> <tr align="left"> <td><a name="wp90738"> </a><div class="pCellBody"><code class="cCode">/bookstore</code></div></td> <td><a name="wp90740"> </a><div class="pCellBody"><code class="cCode">Duke's Bookstore</code></div></td> <td><a name="wp90742"> </a><div class="pCellBody"><code class="cCode">/banner.jsp</code></div></td> <td><a name="wp90744"> </a><div class="pCellBody"><code class="cCode">/bookstore.jsp</code></div></td></tr> <tr align="left"> <td><a name="wp90746"> </a><div class="pCellBody"><code class="cCode">/bookcatalog</code></div></td> <td><a name="wp90748"> </a><div class="pCellBody"><code class="cCode">Book Catalog</code></div></td> <td><a name="wp90750"> </a><div class="pCellBody"><code class="cCode">/banner.jsp</code></div></td> <td><a name="wp90752"> </a><div class="pCellBody"><code class="cCode">/bookcatalog.jsp</code>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -