JSP for Java Web: Custom Tags

Posted by shalinik on Fri, 17 May 2019 00:33:21 +0200

https://www.cnblogs.com/vmax-tam/p/4145334.html

Development steps of custom tags

Step one

Write a common java class that inherits the TagSupport class~

package com.vmaxtam.dotest;
import javax.servlet.jsp.tagext.TagSupport;

public class MyTagTest extends TagSupport {

}

Step two

Rewrite the setPageContext method of the parent class to get the pageContext object of the current jsp page.

public class MyTagTest extends TagSupport {    
    private PageContext pageContext;
    @Override
    public void setPageContext(PageContext pageContext) {
        this.pageContext=pageContext;
    }
}

Step three

Override the doStartTag method of the parent class, which writes the java operation of the tag you defined, where the tag I defined is used to output a large piece of information to the browser:

@Override
    public int doStartTag() throws JspException {

    try {
      pageContext.getResponse().getWriter().write("This is a big piece of information I wrote: ABCDEFGHIJKLMNOPQRSTUVWXYZ");
        } catch (IOException e) {
            e.printStackTrace();
            throw new RuntimeException(e);
        }
        return super.doStartTag();
    }

This completes a label processing program. ~Don't worry, we still need to register it after writing the program.

Step four

In your web application directory, find the WEB-INF folder and create a new tld file in it.

Then register your label in it:

<?xml version="1.0" encoding="ISO-8859-1" ?>
<!DOCTYPE taglib
  PUBLIC "-//Sun Microsystems, Inc.//DTD JSP Tag Library 1.2//EN"
  "http://java.sun.com/dtd/web-jsptaglibrary_1_2.dtd">
<taglib>
  <tlib-version>1.0</tlib-version><!-- Version Number Representing Label Library -->
  <jsp-version>1.2</jsp-version><!-- representative jsp Version -->
  <short-name>mtt</short-name><!-- Short for your tag library -->
  <uri>http://vmaxtam.com/mytag</uri><!-- Reference to your tag library uri -->

  <tag>
      <name>mytah</name><!-- The name of the label you defined -->
       <tag-class>com.vmaxtam.dotest.MyTagTest</tag-class><!-- The corresponding label handler: package name+Class name -->
      <body-content>JSP</body-content><!-- Format of label body content -->
    <!-- Attributes required by tags -->
    <attribute> 
        <name>modelClass</name>
         <required>true</required>
         <rtexprvalue>true</rtexprvalue>
    </attribute>
    <attribute>
        <name>parentId</name>
         <required>true</required>
         <rtexprvalue>true</rtexprvalue>
    </attribute>
  </tag>
 </taglib>

If you forget how to write, you can refer to the tld file in jstl~

Step five

You need to import your tag library using the jsp page of the tag you defined! Just like importing a class package

Just write the following on the jsp page:

<%@taglib uri="http://vmaxtam.com/mytag" prefix="mmt" %>

Step 6

All the preparations have been completed in the above five steps. Let's use the label below.

<html>
  <head>   
    <title>My JSP 'testit.jsp' starting page</title>
  </head>

  <body>
      <mmt:mytag></mmt:mytag>
  </body>
</html>

In this way, we have completed a custom tag. Although we know the steps, we don't know why it works. So, let's talk about its principle.

The Principle of Custom Label

1) When the server opens, it loads the resource files under WEB-INF, including web.xml and tld files, and loads them into memory.

2) We type in the browser http://localhost:8080/TestArea/testit.jsp To access the jsp page

3) The server reads the contents of testit.jsp when it reads

<%@taglib uri="http://vmaxtam.com/mytag" prefix="mmt" %> 

When this sentence is used, it will find whether there is uri in memory. http://vmaxtam.com/mytag The tld file, if not found, will report an error

4) Continue to read the jsp page. When you read the label < mmt: mytag >, you will find the tld file through uri, find out if mytab is defined in the tld file, and then get the content of its tag-class, and find its corresponding tag processing program.

5) Instantiate the label handler and invoke the method in it with the generated object

Here the server also has a certain invocation order for the methods in the label processing program.
A)void setPageContext(PageContext pc) - Pass in pageContext object

B) void setParent (Tag) - If there is a parent tag, the parent tag object is passed in, and if not, null is passed in.

C)int doStartTag() - Called when tag execution begins.

D)int doEndTag() - Called at the end of the label

E)void release() - Release resources

If you do not override the method above, the system will call the method in its parent class.~

Topics: JSP Attribute Java xml