Home > Java > javaTutorial > body text

Introducing a detailed explanation of the usage of javaWeb custom tags

Y2J
Release: 2017-05-02 13:49:06
Original
1527 people have browsed it

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!

Related labels:
source:php.cn
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template