Previous Next |
Creating the ViewThe 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 > <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 /> </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 Example 10-2. A custom tag class for order typespackage 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 toppingspackage 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 "-//Oracle//DTD JSP Tag Library 1.1//EN" "http://java.oracle.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> |
Previous Next |