TagLib application in JSP (4-1)

Source: Internet
Author: User

The key part is as follows. Process tags. In fact, in many cases, we use the provided taglib.

Other people/companies have completed tag and processing. After packaging, all we need to do is apply it in our jsp.

But when we make a taglib, We need to write this part of tag handler.

Here, only the insert tag mentioned in the above file is used. Other values are not described in detail to avoid duplication.

=============================== InsertTag. java ==================================

/*

* $ Id: InsertTag. java, v 1.13 2000/03/04 02:54:57 brydon Exp $

* Copyright 1999 Sun Microsystems, Inc. All rights reserved.

* Copyright 1999 Sun Microsystems, Inc. Tous droits Ré servé s.

*/

Package com. sun. estore. taglib;

Import javax. servlet. jsp. JspTagException;

Import javax. servlet. jsp. tagext. TagSupport;

Import com. sun. estore. util. Debug;

/**

* This class is an easy interface to the JSP template or other

* Text that needs to be inserted.

* @ Author Greg Murray

*/

Public class InsertTag extends TagSupport {

Private boolean directInclude = false;

Private String parameter = null;

Private String templateName = null;

Private Template template = null;

Private TemplateParameter templateParam = null;

/**

* Default constructor

*/

Public InsertTag (){

Super ();

}

Public void setTemplate (String templateName ){

This. templateName = templateName;

}

Public void setParameter (String parameter ){

This. parameter = parameter;

}

Public int doStartTag (){

Try {

If (templateName! = Null ){

Template = (Template) pageContext. getRequest (). getAttribute ("template ");

}

} Catch (NullPointerException e ){

Debug. println ("Error extracting template from session:" + e );

}

If (parameter! = Null & template! = Null) templateParam = (TemplateParameter) template. getParam (parameter );

If (templateParam! = Null) directInclude = templateParam. isDirect ();

Return SKIP_BODY;

}

Public int doEndTag () throws JspTagException {

Try {

PageContext. getOut (). flush ();

} Catch (Exception e ){

// Do nothing

}

Try {

If (directInclude & templateParam! = Null ){

PageContext. getOut (). println (templateParam. getValue ());

} Else if (templateParam! = Null ){

If (templateParam. getValue ()! = Null) pageContext. getRequest (). getRequestDispatcher (templateParam. getValue (). include (pageContext. getRequest (), pageContext. getResponse ());

}

} Catch (Throwable ex ){

Ex. printStackTrace ();

}

Return EVAL_PAGE;

}

}

You can see. InsertTag. java inherits the javax. servlet. jsp. tagext. TagSupport class because some interfaces are defined in TagSupport.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.