Introducing a detailed explanation of the usage of javaWeb custom tags
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.javapackage 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; } }
<?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>
<%@ 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>
<jsp-config> <taglib> <taglib-uri>myhello</taglib-uri> <taglib-location>/WEB-INF/hellotag.tld</taglib-location> </taglib> </jsp-config>
<%@taglib prefix="mytag" uri="myhello" %>
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!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



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

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

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]

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

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