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

Video Face Swap

Video Face Swap

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

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)

Hot Topics

Java Tutorial
1659
14
PHP Tutorial
1257
29
C# Tutorial
1231
24
Is the company's security software causing the application to fail to run? How to troubleshoot and solve it? Is the company's security software causing the application to fail to run? How to troubleshoot and solve it? Apr 19, 2025 pm 04:51 PM

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. ...

How do I convert names to numbers to implement sorting and maintain consistency in groups? How do I convert names to numbers to implement sorting and maintain consistency in groups? Apr 19, 2025 pm 11:30 PM

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

How to simplify field mapping issues in system docking using MapStruct? How to simplify field mapping issues in system docking using MapStruct? Apr 19, 2025 pm 06:21 PM

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...

How does IntelliJ IDEA identify the port number of a Spring Boot project without outputting a log? How does IntelliJ IDEA identify the port number of a Spring Boot project without outputting a log? Apr 19, 2025 pm 11:45 PM

Start Spring using IntelliJIDEAUltimate version...

How to elegantly obtain entity class variable names to build database query conditions? How to elegantly obtain entity class variable names to build database query conditions? Apr 19, 2025 pm 11:42 PM

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...

How to safely convert Java objects to arrays? How to safely convert Java objects to arrays? Apr 19, 2025 pm 11:33 PM

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 to use the Redis cache solution to efficiently realize the requirements of product ranking list? How to use the Redis cache solution to efficiently realize the requirements of product ranking list? Apr 19, 2025 pm 11:36 PM

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...

E-commerce platform SKU and SPU database design: How to take into account both user-defined attributes and attributeless products? E-commerce platform SKU and SPU database design: How to take into account both user-defined attributes and attributeless products? Apr 19, 2025 pm 11:27 PM

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...

See all articles