Day 6

Quiz

A1:

The following custom tag types can be developed for a JSP: simple custom tag; simple custom tag with attributes, Simple Custom Tag with Attributes Defining Scripting Variables, Custom Tag with Body Noninteractive and Interactive tags, and Cooperating Custom Tags.

A2:

For a simple custom tag, the doStartTag(), doEndTag(), and the release() methods need to be overridden. Also, the doEndTag() method must return the constant EVAL_PAGE to continue processing of the JSP page.

A3:

For a simple custom tag with attributes, the doStartTag(), doEndTag(), setAttrib1...N(), getAttrib1...N(), and release() methods need to be overridden. Also, the doEndTag() method must return the constant EVAL_PAGE to continue processing of the JSP page. The same is the case for a Simple Custom Tag with Attributes Defining Scripting Variables.

A4:

To build a custom tag with the body-interactive tag, you need to override the doStartTag(), doInitBody(), doAfterBody(), doEndTag(), and release() methods. Also, the doStartTag() method must return the constant value EVAL_BODY_TAG.

A5:

For implementing a custom tag without a body, the tag handler class needs to either implement the methods of the Tag interface or override the methods of the TagSupport class. A tag handler for a custom tag with a body either implements the methods in the TagBody interface or overrides the methods of the BodyTagSupport class.

Exercises

A1:

Create a Java file called ValidateDateFormat.java which inherits the validation functionality of the CustomizableAdapter class supplied by WebLogic Server to build a custom validator library for JavaServer Pages:

 package weblogicx.jsp.tags.validators;  import javax.servlet.ServletRequest; // Referenced classes of package weblogicx.jsp.tags.validators: //            CustomizableAdapter public class ValidateDateFormat extends CustomizableAdapter {     public ValidateDateFormat()     {     }     public boolean equals(Object obj)     {         if(obj == null)             return false;         if(obj instanceof ValidateDateFormat)         {             ValidateDateFormat validateDateFormat =                     (ValidateDateFormat)obj;             if(getFieldToValidate().equals(validateDateFormat.                     getFieldToValidate()) && getErrorMessage().                     equals(validateDateFormat.getErrorMessage()))                 return true;         }         return false;     }     public boolean validatedate(ServletRequest servletrequest)         throws Exception     {              String correctDateFormat = "MM/DD/YYYY";         String requestDateFormat = super.fieldToValidate;     try {         SimpleDateFormat simpleFormat =               new SimpleDateFormat(correctDateFormat);         simpleFormat.setLenient(false);         Date simpleFormatDate = simpleFormat.parse(requestDateFormat);     }     catch (ParseException e) {         return false;     }     catch (IllegalArgumentException e) {         return false;     }     return true;   } } 

Then, in the taglib.tld of the weblogic-vtags.jar, add the following tag:

 <tag>      <name>validatedate</name>         <tagclass>weblogicx.jsp.tags.validators.ValidateDateFormat                 </tagclass>     <bodycontent>JSP</bodycontent>     <info>         A customized class developed as an extension to the weblogic tag lib     </info>     <attribute>         <name>fieldToValidate</name>         <required>true</required>         <rtexprvalue>true</rtexprvalue>     </attribute>     <attribute>         <name>errorMessage</name>         <required>true</required>         <rtexprvalue>true</rtexprvalue>     </attribute>     <attribute>         <name>expression</name>         <required>false</required>         <rtexprvalue>false</rtexprvalue>     </attribute>     <attribute>         <name>validatorClass</name>         <required>true</required>         <rtexprvalue>true</rtexprvalue>     </attribute> </tag> 

In the ShoppingCartJsp.jsp JSP file that you used in the sample program, include the following code snippet between the display table of the book shopping cart and the submit buttons table:

 <TD><FONT face="Helvetica" size=2>      Date: Enter date in the MM/DD/YYYY format </FONT></td> <TD  width=33% ><FONT face="Helvetica" size=2>     <input type="text" name="dateValue">%> </FONT></TD> <TD> <wl:validatedate    fieldToValidate="dateValue"    validator    errorMessage="Date is not in the proper format"  >     <font color="red">         Date is not in the proper format. Enter it in the MM/dd/yyyy format     </font> </wl:validator></TD> 

Now, deploy the custom tag library by following the steps described today, and your custom validator is ready!

A2:

First create the tag support class called RoundPrice:

 package com.sams.learnweblogic7.tagLibClasses;  import java.io.*; import java.util.*; import javax.servlet.*; import javax.servlet.jsp.*; import javax.servlet.jsp.tagext.*; import com.sams.learnweblogic7.utils.*; /**  * This class displays the amount in the rounded format.  */ public class RoundPrice extends TagSupport{   private double bookPrice;   public int doStartTag() {     System.out.println("Inside the tag in dostart");     try{          JspWriter out = pageContext.getOut();          bookPrice = getBookPrice();          out.println(Utility.getRoundedAmount(bookPrice));     }     catch(Exception e){          System.out.println("Please check your code");               e.printStackTrace();     }     finally{          return(SKIP_BODY);     }   }   public void setBookPrice(double moneyVal){     this.bookPrice = bookPrice;   }   public double getBookPrice(){     return bookPrice;   } } 

Now create the entry in customTagTLD.tld:

 <tag>      <name>RoundPrice</name>     <tagclass>com.sams.learnweblogic7.tagLibClasses.RoundPrice</tagclass>     <bodycontent>empty</bodycontent> </tag> 

Add the following attribute tag in the web.xml file, inside the existing tagLib attribute:

 <attribute>      <name>RoundPrice</name>     <required>false</required>     <rtexprvalue>true</rtexprvalue> </attribute> 

Next you need to alter the JSP. The book price will be calculated via the following:

 double totBook1Price = book1Qty * book1.getBookPrice();  double totBook2Price = book2Qty * book2.getBookPrice(); double totBook3Price = book3Qty * book3.getBookPrice(); double grandTotal = totBook1Price + totBook2Price + totBook3Price; 

Now the code to display the table in the ViewCartJsp.jsp will look as follows:

 <wl:cache name="commonHeader" scope="session">  </wl:cache>                <CENTER><B>Your Shopping Cart Contains:</B></CENTER><BR> <TABLE width = 100%>    <TR>       <TH>&nbsp;</TH>       <TH>Book Name and description</TH>       <TH>Book Price</TH>       <TH>Qty</TH>       <TH>Total for each book</TH>    </TR> <wl:cache name="book1Vars" scope="session"></wl:cache>       <TD align='center'><%=book1Qty%></TD>       <TD align='center'>          $<ctt:RoundPrice BookPrice="<%=totBook1Price%>"/>       </TD>    </TR> <wl:cache name="book2Vars" scope="session"></wl:cache>       <TD align='center'><%=book2Qty%></TD>       <TD align='center'>          $<ctt:RoundPrice BookPrice="<%=totBook2Price%>"/>       </TD>    </TR> <wl:cache name="book3Vars" scope="session"></wl:cache>       <TD align='center'><%=book3Qty%></TD>       <TD align='center'>         $<ctt:RoundPrice BookPrice=               "<%=totBook3Price%>"/></FONT>       </TD>    </TR>    <TR bgcolor='#eeeeee'>       <TD>&nbsp;</TD>       <TD>&nbsp;</TD>       <TD>&nbsp;</TD>       <TD>&nbsp;</TD>       <TD align='left'>           <B>Total=$<ctt:RoundPriceBookPrice="<%=grandTotal%>"/></B>       </TD>    </TR> </TABLE> <p align ="left">    <INPUT type="submit" name="buttonName" value="Empty Shopping Cart"> </p> 



Sams Teach Yourself BEA WebLogic Server 7. 0 in 21 Days
Sams Teach Yourself BEA WebLogic Server 7.0 in 21 Days
ISBN: 0672324334
EAN: 2147483647
Year: 2002
Pages: 339

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