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

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

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











Troubleshooting and solutions to the company's security software that causes some applications to not function properly. Many companies will deploy security software in order to ensure internal network security. ...

Solutions to convert names to numbers to implement sorting In many application scenarios, users may need to sort in groups, especially in one...

Field mapping processing in system docking often encounters a difficult problem when performing system docking: how to effectively map the interface fields of system A...

Start Spring using IntelliJIDEAUltimate version...

When using MyBatis-Plus or other ORM frameworks for database operations, it is often necessary to construct query conditions based on the attribute name of the entity class. If you manually every time...

Conversion of Java Objects and Arrays: In-depth discussion of the risks and correct methods of cast type conversion Many Java beginners will encounter the conversion of an object into an array...

How does the Redis caching solution realize the requirements of product ranking list? During the development process, we often need to deal with the requirements of rankings, such as displaying a...

Detailed explanation of the design of SKU and SPU tables on e-commerce platforms This article will discuss the database design issues of SKU and SPU in e-commerce platforms, especially how to deal with user-defined sales...
