Home Java javaTutorial Introducing a detailed explanation of the usage of javaWeb custom tags

Introducing a detailed explanation of the usage of javaWeb custom tags

May 02, 2017 pm 01:49 PM
javaweb Custom labels

This article mainly introduces the usage of javaWeb custom tags, and analyzes the functions, definition methods and execution principles of javaweb custom tags in the form of examples. Friends in need can refer to the following

The examples in this article describe javaWeb Custom label usage. Share it with everyone for your reference, the details are as follows:

Custom tag creation

Custom tags are mainly used to remove JSP pages Java code.
To remove the java code in the jsp page, you only need to complete two steps:
-Write a Java class that inherits TagSupport, override the doStartTag method, and write the java code in the jsp page into the doStartTag method.
- Write a tag library descriptor (tld) file and describe the custom tag in the tld file.
After completing the above operations, you can import and use custom tags in JSP pages.

Tag processing class: HelloTag.java
Tag description file: hellotag.tld
jsp page call: <%@taglib%>Define emoticon
[Optional] in web. Configure hellotag.tld mapping in xml

Application process:

index.jsp ==>[web.xml]==>hellotag.tld==> ;HelloTag.java

Define the tag support class as follows:

##HelloTag.java

package china.hubei;
import java.io.IOException;
import javax.servlet.jsp.*;
import javax.servlet.jsp.tagext.TagSupport;
//自动定义标签
public class HelloTag extends TagSupport {
 public int doStartTag() throws JspException{
   PageContext pg=(PageContext)super.pageContext;
   JspWriter out=pg.getOut();
   try{
     out.println("hello world");
   }catch(IOException e){
   }
   return TagSupport.SKIP_BODY;
 }
}
Copy after login

Tag description class, the suffix is ​​.tld, and the content conforms to xml syntax rules

hellotag.tld

<?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 web-jsptaglibrary_2_0.xsd"
  version="2.0">
    <tlib-version>1.0</tlib-version> <!-- 标签库的版本 -->
    <short-name>dqtag</short-name><!-- 标签库在TLD中的描述名称 -->
    <tag>
      <name>hello</name>   <!-- 标签在jsp中使用名称 -->
      <tag-class>china.hubei.HelloTag</tag-class><!-- 标签指向的class文件 -->
      <body-content>empty</body-content><!-- 标签内容为空 -->
    </tag>
</taglib>
Copy after login

Use custom tags on jsp pages

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@ page isELIgnored="false"%>
<%@taglib prefix="mytag" uri="/WEB-INF/hellotag.tld" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
 <head>
 <title>标题</title>
  <!--
  <link rel="stylesheet" type="text/css" href="styles.css" rel="external nofollow" >
  -->
  <script language="javascript">
  </script>
 </head>
 <body>
  <h1><mytag:hello /></h1>
 </body>
</html>
Copy after login

The execution effect is as follows:

Custom label improvements, move the label description file in the tld file to the web.xml file. Just make a mapping for the tld file in the web.xml file.

That is, add the following content to web.xml:

<jsp-config>
  <taglib>
    <taglib-uri>myhello</taglib-uri>
    <taglib-location>/WEB-INF/hellotag.tld</taglib-location>
  </taglib>
</jsp-config>
Copy after login

The introduction tag in index.jsp is changed to the following:

<%@taglib prefix="mytag" uri="myhello" %>
Copy after login

Remarks:

Execution principle of custom tags

When the JSP engine encounters a custom tag, it first creates an instance object of the tag processor class , and then call its methods in sequence according to the communication rules defined by the JSP specification.

1. public void setPageContext(PageContext pc). After the JSP engine instantiates the tag processor, it will call the setPageContext method to pass the pageContext object of the JSP page to the tag processor. The tag processor can later use this pageContext object. Communicate with JSP pages.

2. public void setParent(Tag t). After the setPageContext method is executed, the WEB container then calls the setParent method to pass the parent tag of the current tag to the current tag processor. If the current tag has no parent tag, it is passed to setParent. The parameter value of the method is null.
3. public int doStartTag(), after calling the setPageContext method and setParent method, when the WEB container executes the start tag of the custom tag, it will call the doStartTag method of the tag processor.
4. public int doEndTag(). After the WEB container executes the tag body of the custom tag, it will then execute the end tag of the custom tag. At this time, the WEB container will call the doEndTag method of the tag processor.
5. public void release(). Usually after the WEB container executes the custom tag, the tag processor will reside in the memory and serve other requests. The web container will not call the release method until the web application is stopped.

The above is the detailed content of Introducing a detailed explanation of the usage of javaWeb custom tags. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
1 months ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
1 months ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
1 months ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Chat Commands and How to Use Them
1 months ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How does Java's classloading mechanism work, including different classloaders and their delegation models? How does Java's classloading mechanism work, including different classloaders and their delegation models? Mar 17, 2025 pm 05:35 PM

Java's classloading involves loading, linking, and initializing classes using a hierarchical system with Bootstrap, Extension, and Application classloaders. The parent delegation model ensures core classes are loaded first, affecting custom class loa

How do I implement multi-level caching in Java applications using libraries like Caffeine or Guava Cache? How do I implement multi-level caching in Java applications using libraries like Caffeine or Guava Cache? Mar 17, 2025 pm 05:44 PM

The article discusses implementing multi-level caching in Java using Caffeine and Guava Cache to enhance application performance. It covers setup, integration, and performance benefits, along with configuration and eviction policy management best pra

How can I use JPA (Java Persistence API) for object-relational mapping with advanced features like caching and lazy loading? How can I use JPA (Java Persistence API) for object-relational mapping with advanced features like caching and lazy loading? Mar 17, 2025 pm 05:43 PM

The article discusses using JPA for object-relational mapping with advanced features like caching and lazy loading. It covers setup, entity mapping, and best practices for optimizing performance while highlighting potential pitfalls.[159 characters]

How do I use Maven or Gradle for advanced Java project management, build automation, and dependency resolution? How do I use Maven or Gradle for advanced Java project management, build automation, and dependency resolution? Mar 17, 2025 pm 05:46 PM

The article discusses using Maven and Gradle for Java project management, build automation, and dependency resolution, comparing their approaches and optimization strategies.

How do I create and use custom Java libraries (JAR files) with proper versioning and dependency management? How do I create and use custom Java libraries (JAR files) with proper versioning and dependency management? Mar 17, 2025 pm 05:45 PM

The article discusses creating and using custom Java libraries (JAR files) with proper versioning and dependency management, using tools like Maven and Gradle.

See all articles