10.2 Creating the View


The first file that the user navigates to is the view file Ch10_01.jsp . In this file, we use various custom Struts tags to implement the display you see in Example 10-1. For example, the <html:form> tag creates a Struts-enabled form that can display controls, as you see in Example 10-1; we're setting the form's action attribute to the name we'll give the controller, Ch10_04.do , so when the user clicks the Submit button (with the caption "Place your order"), the data in the form will be forwarded to the controller.

Example 10-1. A sample JSP
 <%@ page contentType="text/html;charset=UTF-8" language="java" %> <%@ taglib uri="/WEB-INF/struts-bean.tld" prefix="bean" %> <%@ taglib uri="/WEB-INF/struts-html.tld" prefix="html" %> <%@ taglib uri="/WEB-INF/struts-logic.tld" prefix="logic" %> <%@ taglib uri="/Ch10" prefix="Ch10" %> <HTML>     <HEAD>         <TITLE>Here's the menu...</TITLE>     </HEAD>          <BODY>         <H1>Here's the menu...</H1>         <html:errors/>         <Ch10:type/>         <Ch10:items/>  <html:form action="Ch10_04.do">             <TABLE>                 <TR>                     <TD ALIGN="LEFT" VALIGN="TOP">                         <bean:message key="items"/>                         <BR>                         <logic:iterate id="items1" name="items">                             <html:multibox property="items">                                 <%= items1 %>                             </html:multibox>                             <%= items1 %>                             <BR>                         </logic:iterate>                     </TD>                     <TD ALIGN="LEFT" VALIGN="TOP">                         <bean:message key="type"/>                         <BR>                         <html:select property="type">                             <html:options name="type"/>                         </html:select>                     </TD>                 </TR>                 <TR>                     <TD ALIGN="LEFT">                         <BR>                         <bean:message key="email"/>                         <html:text property="email"/>                     </TD>                 <TR>             </TABLE>                   <BR>             <html:submit value="Place your order"/>         </html:form>  </BODY> </HTML> 

Create this file by right-clicking the deployment folder in Eclipse and selecting New File, which automatically stores Ch10_01.jsp in the webapps/Ch10_01 folder.

As you see in Figure 10-1, the drop-down list we're presenting holds the items Pizza, Calzone, and Sandwich, and you can see the list of ingredientsSausage, Cheese, Pepperoni, Meatballs, and Peppersrepresented with a list of checkboxes. To make the items in these lists available to Struts HTML control in the view, we'll use two custom JSP tags, <Ch10:type> to return items like Pizza and Calzone, and <Ch10:items> to return items like Sausage, Cheese, and Pepperoni. As you can see in Example 10-1, we use the Struts <logic:iterate> , <html:multibox> , and <html:options> tags to create the needed HTML controls from those lists of items. You can see the implementation of these custom tags in Example 10-2 and Example 10-3. In Eclipse, create these files and store them in the src folder by right-clicking that folder and selecting New Class, placing the new classes, Ch10_02 and Ch10_03 , in the org.eclipsebook.ch10 package.

Example 10-2. A custom tag class for order types
 package org.eclipsebook.ch10; import javax.servlet.jsp.tagext.TagSupport; public class Ch10_02 extends TagSupport  {     public int doStartTag( )        {  String[] typeArray = {"", "Pizza", "Calzone", "Sandwich"};                  pageContext.setAttribute("type", typeArray);  return SKIP_BODY;     } } 
Example 10-3. A custom tag class for pizza toppings
 package org.eclipsebook.ch10; import javax.servlet.jsp.tagext.TagSupport; public class Ch10_03 extends TagSupport  {    public int doStartTag( )       {  String[] itemsArray = {"Sausage", "Cheese", "Pepperoni", "Meatballs", "Peppers"};                 pageContext.setAttribute("items", itemsArray);  return SKIP_BODY;    } } 

To make these custom tags work, we need a tag library descriptor file, which you can see in Example 10-4. This file goes into deployment\WEB-INF .

Example 10-4. The TLD for the custom tags
 <?xml version="1.0"?> <!DOCTYPE taglib PUBLIC "-//Sun Microsystems, Inc.//DTD JSP Tag Library 1.1//EN"     "http://java.sun.com/j2ee/dtds/web-jsptaglibrary_1_1.dtd"> <taglib>     <tlibversion>1.0</tlibversion>     <jspversion>1.2</jspversion>     <shortname>StrutsExample</shortname>     <info>         Supports the Struts Example     </info>  <tag>         <name>type</name>         <tagclass>org.eclipsebook.ch10.Ch10_02</tagclass>         <bodycontent>empty</bodycontent>    </tag>        <tag>         <name>items</name>         <tagclass>org.eclipsebook.ch10.Ch10_03</tagclass>         <bodycontent>JSP</bodycontent>     </tag>  </taglib> 

ISBN: 0596006411
EAN: 2147483647
Year: 2006
Pages: 114
Authors: Steve Holzner

Similar book on Amazon

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net