JSP custom Tags simple Getting Started Tutorial _jsp programming

Source: Internet
Author: User
Tags tld

There are a few words on the Sun's official document.

Official Document statement

 public interface Simpletag extends Jsptag interface for defining simple Tag handlers. S Imple tag handlers differ from Classic tag handlers into that instead of supporting doStartTag () and Doendtag (), the Simplet AG interface provides a simple Dotag () method, which are called once and only once for any given tag invocation. All tags logic, iteration, body evaluations, etc. are to is performed in.

Thus, simple tag handlers have the equivalent power of bodytag, but with a much simpler lifecycle and interface. To support body content, the Setjspbody () is provided. The container invokes the Setjspbody () method and a Jspfragment object encapsulating the body of the tag.

The tag handler implementation can call Invoke () on this fragment to evaluate the body as many times as it needs. A simpletag handler must have a public No-args constructor.
Most Simpletag handlers should extend Simpletagsupport. 

Life cycle and call process

The following is a non-normative, brief overview of the Simpletag lifecycle. Refer to the JSP specification for details.

A new Tag Handler instance is created the container by calling the provided Zero-args. Unlike classic tag handlers, simple tag handlers are never-cached and reused by the JSP container.
The Setjspcontext () and SetParent () methods are called by the container. The SetParent () method was only called if the element is nested within another tag invocation.
The setters for each attribute defined to this tag are called by the container.
If a body exists, the setjspbody () is called by the container to set the body of this tag, as a jspfragment. If the action element is empty on the page, this is not called at all.
The Dotag () is called by the container. All tags logic, iteration, body evaluations, etc. occur.
The Dotag () method is returns and all variables are synchronized.

Simple label Use small case

Must know: The simple label is also a label, so the process of declaring the same as tag, is also three steps.

1, constructs inherits the Simpletag class the realization class, the rewriting Dotag method
2, the TLD file in a strict statement
3, JSP page taglib namespace and tag prefix declaration, and then invoke the custom simple label

First step: Create the Implementation class:

Package Web.simpletag;
Import java.io.IOException;

Import Java.io.StringWriter;
Import javax.servlet.jsp.JspException;
Import Javax.servlet.jsp.PageContext;
Import javax.servlet.jsp.SkipPageException;
Import javax.servlet.jsp.tagext.JspFragment;


Import Javax.servlet.jsp.tagext.SimpleTagSupport; /** * Controls whether the label body executes * @author Summer * */public class Bodycontroller extends Simpletagsupport {static{* * * The single label overall execution process is as follows: * 1. The browser sends a request to the Web server, and the Web server invokes the servlet (JSP) * 2.complier interpreter for initialization, calling the Setjspcontext method first, and PageContext the Like a pass in * 3. Then look at the parent tag of this tag, that is, the SetParent method * 4. And then call the Dotag method?
  But to know that Dotag will use the Jspfragment object, you must get it first, so call Setjspbody (Jspfragment jspbody) method * 5. Finally, the Dotag method is invoked to execute the relevant code logic. /** * Simple tags can use this method to implement all business logic * * * @Override public void Dotag () throws Jspexception, IOException {//representative Mark
    The object of the Jspfragment fragment = This.getjspbody (); Fragment.invoke (null), refers to the contents of the label to whom, NULL on behalf of the browser//1. Modify the contents of the label//Fragment.invoke (null); 
    2. Control the label body content of the duplicate output//for (int i=1;i<=5;i++) {//Fragment.invoke (NULL);//set to NULL, default to browser output//}//3. Modify the contents of the label body
    PageContext context = (PageContext) fragment.getjspcontext ();
    StringWriter writer = new StringWriter ();
    Fragment.invoke (writer);

    String content = Writer.getbuffer (). toString ();

    This.getjspcontext (). Getout (). Write (Content.touppercase ()); 4. Control the implementation of JSP pages, only need to master a principle can * * skippageexception-if the page that (either directly or indirectly) invoked th is * tag is to cease evaluation.  A simple tag Handler generated from a tag * file must throw this exception if a invoked Classic Tag Handler *  Returned Skip_page or if a invoked simple Tag Handler threw * skippageexception or if a invoked Jsp Fragment
     A * skippageexception.
  *///throw new Skippageexception ();
 }


}

To configure the related constraint items in a TLD file:

<?xml version= "1.0" encoding= "UTF-8"?> <taglib xmlns= "HTTP://JAVA.SUN.COM/XML/NS/J2EE"

Http://www.w3.org/2001/XMLSchema-instance "
  xsi:schemalocation=" http://java.sun.com/xml/ns/j2ee http:// Java.sun.com/xml/ns/j2ee/web-jsptaglibrary_2_0.xsd "
  version=" 2.0 ">

  <DESCRIPTION>JSTL 1.1 xml library</description>
  <display-name>jstl xml</display-name>
  <tlib-version>1.1 </tlib-version>
  <short-name>x</short-name>
  <uri>/simplesummer</uri>


  <!--a custom label for a simple label that controls the contents of the label-->
  <tag>
    <name>BodyController</name>
    < Tag-class>web.simpletag.bodycontroller</tag-class>
    <body-content>scriptless</ body-content>
  </tag>
</taglib>

Step three: make a declaration in the JSP page and call:

<%@ page language= "java" contenttype= "text/html; Charset=utf-8 "
  pageencoding=" UTF-8 "%>
<% @taglib uri="/simplesummer "prefix=" Summer "%>
<! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" "Http://www.w3.org/TR/html4/loose.dtd" >
 
 

Summarize:
simple tags can do the same thing instead of the Bodytag interface, but have a much simpler and lighter
Simple label lifecycle logic clear, call rules clear
The control of the label body can be accomplished by using the related flow object maniplate

The above is the entire content of this article, I hope to help you learn.

Related Article

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.