JSP custom tag simple getting started tutorial, jsp custom getting started tutorial

Source: Internet
Author: User
Tags tld

JSP custom tag simple getting started tutorial, jsp custom getting started tutorial

The following is the official sun document.

Official Document statement

public interface SimpleTagextends JspTagInterface for defining Simple Tag Handlers.Simple Tag Handlers differ from Classic Tag Handlers in that instead of supporting doStartTag() and doEndTag(), the SimpleTag interface provides a simple doTag() method, which is called once and only once for any given tag invocation. All tag logic, iteration, body evaluations, etc. are to be performed in this single method. Thus, simple tag handlers have the equivalent power of BodyTag, but with a much simpler lifecycle and interface.To support body content, the setJspBody() method is provided. The container invokes the setJspBody() method with a JspFragment object encapsulating the body of the tag. The tag handler implementation can call invoke() on that 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 each time by the container by calling the provided zero-args constructor. 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 is only called if the element is nested within another tag invocation.
The setters for each attribute defined for this tag are called by the container.
If a body exists, the setJspBody () method is called by the container to set the body of this tag, as a JspFragment. if the action element is empty in the page, this method is not called at all.
The doTag () method is called by the container. All tag logic, iteration, body evaluations, etc. occur in this method.
The doTag () method returns and all variables are synchronized.

Simple tag use case

Required: A simple Tag is also a Tag, so the declaration process is the same as that of a three-step Tag.

1. Create an implementation class that inherits the SimpleTag class and override the doTag method.
2. Strict statements in tld files
3. Declare the namespace and tag prefix of taglib on the jsp page, and then call the custom simple tag.

Step 1:Create 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;/*** control whether the label body is executed * @ author Summer **/public class BodyController extends SimpleTagSupport {static {/** overall execution of simple labels The procedure is as follows: * 1. the browser sends a request to the web server and then the web server calls servlet (jsp) * 2. the complier interpreter initializes the object by calling the setJspContext method and passing the pageContext object to the object. then, let's look at the parent tag of this tag, that is, the setParent Method * 4. is the doTag method called? But you need to know that the doTag uses the JspFragment object internally, so you must obtain it first. Therefore, you should call the setJspBody (JspFragment jspBody) Method * 5. finally, call the doTag method and execute the relevant code logic */}/*** simple tag. You can use this method to implement all the business logic */@ Override public void doTag () throws JspException, IOException {// indicates the object JspFragment fragment = this. getJspBody (); // fragment. invoke (null); refers to who writes the content in the tag. null indicates the browser // 1. modify the content of the label body // fragment. invoke (null); // 2. control repeated output of TAG body content // for (int I = 1; I <= 5; I ++) {// fragment. invoke (null); // set to null, Which is output to the browser by default. //} // 3. modify the content PageContext context = (PageContext) fragment of the label body. getJspContext (); StringWriter writer = new StringWriter (); fragment. invoke (writer); String content = writer. getBuffer (). toString (); this. getJspContext (). getOut (). write (content. toUpperCase (); // 4. to control the execution of jsp pages, you only need to master one principle./** SkipPageException-If the page that (either directly or indirectly) invoked this * tag is to cease evaluation. A Simple Tag Handler generated from a tag * file must throw this exception if an invoked Classic Tag Handler * returned SKIP_PAGE or if an invoked Simple Tag Handler threw * SkipPageException or if an invoked Jsp Fragment threw a * SkipPageException. * // throw new SkipPageException ();}}

Configure related constraints in the tld file:

<? Xml version = "1.0" encoding = "UTF-8"?> <Taglib xmlns = "http://java.sun.com/xml/ns/j2ee" xmlns: xsi = "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 & gt; 1.1 </tlib-version> <short-name> x </short-name> <uri>/simplesummer </uri> <! -- Control the custom label of the simple label of the label body --> <tag> <name> BodyController </name> <tag-class> web. simpletag. bodyController </tag-class> <body-content> scriptless </body-content> </tag> </taglib>

Step 3:Declare on the jsp page and then 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"> 

Summary:
Simple tags can replace the BodyTag interface to perform the same operation, but they are simpler and lighter.
Simple label lifeCycle with clear logic and clear call rules
Maniplate

The above is all the content of this article, hoping to help you learn.

Articles you may be interested in:
  • Detailed description of jsp struts1 tag instances
  • Summary of the implementation process of JSP custom tag Taglib
  • Use of common jsp labels
  • Jsp custom tag Technology (implementation principles, code, and platform building steps)
  • How to obtain user IP addresses using JSP custom tags
  • How can I implement cascade using the select tag on a JSP page?
  • Jsp base tag and meta tag learning Summary
  • ForEach traversal and escape character examples of simple custom jsp labels
  • How to format a timestamp string as a time tag in a jsp page
  • JSP custom pagination TAG

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.