Saturday, March 1, 2008

Steps in Making use of a Tag Library Validator

Step 1: To use a tag library validator, we need to have a Tag library descriptor
(TLD) file first


What exactly is a TLD file?
-- TLD file is an xml file which describes what restrictions apply to the JSP page in which it is referred.

Where to place the TLD file?
--Under WEB_INF/lib in a dynamic web project under Eclipse.

What does it have?
-- XML header describing the deployment descriptor DOCTYPE. The deployment descriptor
includes the elements and configuration information of a web application.
             PUBLIC "-//Sun Microsystems, Inc.//DTD JSP Tag Library 1.1//EN"
"http://java.sun.com/j2ee/dtds/web-jsptaglib_1_1.dtd">
--Root element is taglib which contains element validator(not the only child ele)

What does a Validator Element look like?
--
validator--root element
validator-class as child
with template text javax.servlet.jsp.jstl.tlv.ScriptFreeTLV

init-param element /s as other children
param-name : allowScriptlets //To disallow scriptlets
param-value: false

The mandatory tag child
name: MyTag
tag-class: tags.MyTagAction //Refer step 2

Step 2: A class that supports the mandatory tag -- The tag handler class (goes
under /webapp/src folder)


import javax.servlet.jsp.*;
import javax.servlet.jsp.tagext.*;

public class MyTagAction extends TagSupport //Extends Tag--can also use BodyTagSupport
{
static final long serialVersionUID =1;
public int doStartTag() throws JspException
{
//Other Tag handler Methods are doEndTag,release
return SKIP_BODY; // Can also have EVAL_BODY_TAG or EVAL_BODY_INCLUDE
}
}

Further Info here:
http://jakarta.apache.org/taglibs/tutorial.html#basic_tag_library

Step 3: Modify the web.xml file to help the container identify the taglib

Add this to the web.xml file in the webapp.

--jsp-config element
-- taglib element
--taglib-uri-- http://java.sun.com/sample_uri --/taglib-uri--
--taglib-location-- /WEB-INF/restrict.tld --/taglib-location--
--/taglib--
--/jsp-config--


Q: Why have taglib as jsp-config's child?
Refer:http://www.myeclipseide.com/PNphpBB2-viewtopic-t-5656.html

Step 4: Testing the restrictions in a JSP file

<%@ taglib uri="http://java.sun.com/sample_uri" prefix="test"%>

If we do use a scriptlet/s in this JSP page now, the validation fails and the error message
is:

org.apache.jasper.JasperException:


Validation error messages from TagLibraryValidator for Tag library descriptor in /parser.jsp

null: JSP page contains 2 scriptlets.


That's it for now folks...

--Phani

No comments: