"Head First Servlets & JSP" -10-Custom Tag development

Source: Internet
Author: User
Tags tld

Tag file: Want to include, but better than include
    • The simplest way to create and use markup files
    1. Take a contained file (such as header.jsp) and rename it with a. tag extension (header.tag);
    2. Place the tag file (Header.tag) in a directory named tags in the Web-inf directory;
    3. Put a taglib directive (with a Tagdir attribute) in the JSP and call this tag;
    • How do I send parameters to a markup file?
      For tag files, send not request parameters, but tag attributes!
      To invoke tokens in a JSP:

      To use attributes in a markup file:

    • How to define the required attributes in a markup file--using the attribute directive
      The attribute directive can only be used by markup files, which are somewhat similar to the attribute child elements in the tag section of a custom TLD.

      If the property value is large, the attribute is removed from the tag file as the body. That is, do not attribute the instructions in the markup file:

    • If there is no TLD, how do you declare the contents of the body? --TAG directive
      There are not only attribute directives in the tag file, but also tag directives, and the tag directive has the Body-content attribute to declare the type of the body content of the tag file. The value of the Body-content property has the following three types:
scriptless (default) tagdependent Empty
Scripting not allowed Mark body as plain text There's nothing in the tag body.

In fact, scriptlet scripts (which can have El expressions) are never allowed in the body of markup file tags.

    • Where does the container find the tag file?
      The container looks for a tag file in 4 locations:
    1. Find directly in the Web-inf/tags directory
    2. Find in subdirectories of Web-inf/tags
    3. Find in the Meta-inf/tags directory of the jar file under Web-inf/lib
    4. Look in the meta-inf/tags subdirectory of the jar file under Web-inf/lib

      Note that if the tag file is deployed in a jar, the tag file must have a TLD, and if directly in the Web application, no TLD is required.

If the tag file is not enough, you sometimes need Java

So far, using tag files, non-Java programmers can also build custom tags. There is no feature that involves a Java class to handle markup, nor does it involve establishing a TLD for a tagged file.
However, if you want to write your own markup file can be called by others as a jar, then you must have a TLD, or even tag processor (Java Class).

    • Tag processor
      The tag processor differs from the tag file, which is the Java class that completes the markup work, somewhat similar to the El function, but more powerful and more flexible.
      The El function is just a static method, and the tag processor class can access the tag attribute, the tag body, and even the page context to get scope properties and requests and responses.
    • Two types of custom tag processors: simple and traditional
      In general, simple types can meet almost all needs.
Create a simple tag processor
    • General steps
    1. Write a class that extends Simpletagsupport
    2. Implementing the Dotag () method
    3. Create a TLD for a tag
    4. Deploying tag processors and TLDs
    5. Write a JSP that uses markup

    • An example



The life cycle of a simple tag processor

Customize a simple tag with attributes (like C:foreach? )

Stop page Processing--skippageexception

Stop page processing, not just throwing exceptions, but expecting the first part of the page to be processed as a response, but not the latter part of the process for some reason.


The page information after the visible tag (back in the page ...) has no output!

What happens if a tag is called from a contained page?

Skippageexception just stops calling the tagged page directly, and the outer page continues to execute.

Key points of a simple tag

The difference between a simple tag and a traditional marker

Traditional markers Compare simple tags to see:

Head First Servlets & JSP -10-Custom Markup development

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.