当前位置:网站首页>Custom JSP tag - > concept - > lifecycle
Custom JSP tag - > concept - > lifecycle
2022-06-11 23:50:00 【ˡ A cup of American style】
1: What is a label
The so-called tag is actually markup language , Is an annotated text language , The computer can be operated easily . To put it bluntly, it is a language that can be recognized by the computer
2:jsp What is the tag library
It's a JSP Label set , It encapsulates the JSP Common core functionality for applications , be based on JSP The label can be understood as , yes JSP It should be a way of encapsulating common functions . You can look directly at JSP Label set , It can be more convenient for us to use JSP label
3:jsp What labels can do
adopt JSP Tags can make web pages concise and more convenient for us to maintain , It is also convenient to realize the same JSP The file supports multiple language versions ( To put it bluntly JSP Tags are a cross platform language , Other languages can be used ).
JSP Explanation of label life cycle
When we instantiate the tag helper class, we can start through doStartTag Method to handle JSP label , When we send a response , stay doStartTag The internal will start to judge whether there is a label body . If there is a label body, it will enter the doAFterBody In the method , stay doAFterBody The method will continue to judge whether there is a method body , If there is still a method body in it, it will continue to execute doAFterBody Method . When doAFterBody When the tag body in the method is processed, it will execute doEndTag, Such a JSP The life cycle is over Be careful :JSP The label is through Java Reflection 
technological process A:
SKIP_BODY
3.1 Instantiate the tag helper class ->doStartTag()------------->doEndTag()
// Mainly used to develop simple labels
technological process B:
EVAL_BODY_INCLUDE SKIP_BODY
3.2 Instantiate the tag helper class ->doStartTag()------------->doAfterBody---------------->doEndTag()...
EVAL_BODY_AGAIN
doStartTag
doStartTag Encounter method JSP How to start processing labels , It will have two return values . One is SKIP_BODY, The other is EVAL_BODY_INCLUDE.SKIP_BODY Indicates that the text between labels is not displayed ,EVAL_BODY_INCLUDE Indicates the text between the display labels
doAFterBody
doAFterBody The method is to respond after displaying the text between labels , Its return value is EVAL_BODY_AGAIN And SKIP_BODY.EVAL_BODY_AGAIN Indicates that the text between labels will be displayed again ,SKIP_BODY Indicates to perform the next step
doEndTag
doEndTag The method is processed at the end of the tag , Its return value is EVAL_PAGE And SKIP_PAGE.EVAL_PAGE Indicates that after processing the current tag, continue to execute the following JSP Webpage ,SKIP_PAGE Indicates that the following... Will not be processed JSP Webpage
1. Tag language features
< The start tag attribute =" Property value "> Tagging body </ End tag >
Empty label
<br/><hr/>
< The start tag ></ End tag >
< The start tag />
2. Development and use steps of custom labels ( Browsers use :google/firefox)
Write helper classes
2.1 Create a label helper class ( Inherit BodyTagSupport)
The tag property must correspond to the property of the helper class 、 And corresponding information shall be provided get/set Method
rtexprvalue
package com.zking.jsptag.tag;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.BodyTagSupport;
/**
* Custom label development steps
* 1) Create a label helper class ( Inherit BodyTagSupport)
* notes : The tag property must correspond to the property of the helper class 、 And corresponding information shall be provided get/set Method
* 2) Create a label library description file (tld), Must be placed in WEN-INF Or its subdirectories
*
* 3) On the page through taglib Instruction to introduce custom tag library
* @author Administrator
*
*/
public class TestTag extends BodyTagSupport {
private Object name;
private int count=0;
public Object getName() {
return name;
}
public void setName(Object name) {
this.name = name;
}
/**
* doStartTag(): Express < The start tag > The corresponding action to be performed
* for example : Corresponding <z:test> Actions performed by the label
* Return value :
* 1)SKIP_BODY: Skip body content and do not execute
* 2)EVAL_BODY_INCLUDE: Calculate the body content and include it in the output
*/
@Override
public int doStartTag() throws JspException {
// TODO Auto-generated method stub
count=0;
System.out.println("name="+this.name);
System.out.println("doStartTag(): Express < The start tag > The corresponding action to be performed ");
return EVAL_BODY_INCLUDE;
}
/**
* doAfterBody(): Be situated between < The start tag > Tagging body < End tag > The actions performed between
* for example : Be situated between <z:test>888 And </z:test> The actions performed between
* Return value :
* 1):SKIP_BODY: Skip body content without outputting ( It can be understood here as for In the loop break)
* 2):EVAL_BODY_AGAIN: Calculate the body content again and include it in the output ( It can be understood as continue)
*/
@Override
public int doAfterBody() throws JspException {
// TODO Auto-generated method stub
System.out.println("doAfterBody(): Be situated between < The start tag > Tagging body < End tag > The actions performed between ");
//count=3 No output
if(count<3) {
count++;
return EVAL_BODY_AGAIN;
}
return SKIP_BODY;
}
/**
* doEndTag(): Express < End tag > The corresponding action to be performed
* for example : Corresponding </z:test> The object executed
* Return value :
* 1):SKIP_PAGE: Skip the rest of the page
* 2):EVAL_PAGE: Calculate the subsequent content of the page
*/
@Override
public int doEndTag() throws JspException {
// TODO Auto-generated method stub
System.out.println("doEndTag(): Express < End tag > The corresponding action to be performed ");
return EVAL_PAGE;
}
}
Writing a helper class is actually defining an entity class , Just three more implementations JSP Label method .
Write tag library description file
2.2 Create a label library description file (tld), Add custom label configuration
notes :tld The file must be saved to WEB-INF Directory or its subdirectories
jstl Tag library
<!DOCTYPE taglib
PUBLIC "-//Sun Microsystems, Inc.//DTD JSP Tag Library 1.2//EN"
"http://java.sun.com/dtd/web-jsptaglibrary_1_2.dtd">
<!-- Tag library descriptor -->
<taglib xmlns="http://java.sun.com/JSP/TagLibraryDescriptor">
<!-- Represents the version number of the tag library -->
<tlib-version>1.0</tlib-version>
<!-- representative jsp Version of -->
<jsp-version>1.2</jsp-version>
<!-- The abbreviation of your tag library -->
<short-name>z</short-name>
<!-- Reference to your tag library uri -->
<uri>/zking</uri>
<tag>
<!-- Tag name -->
<name>test</name>
<!-- Label tool class -->
<!-- Class.forName("com.zking.jsptag.tag.TestTag") -->
<tag-class>com.zking.jsptag.tag.TestTag</tag-class>
<!-- The content type of the tag :empty Represents an empty label ,jsp It can be any legal JSP Elements -->
<body-content>jsp</body-content>
<!-- Custom label attribute definitions , Please note that the corresponding... Must be provided in the tag class get/set Method -->
<attribute>
<!-- The attribute name of the custom tag -->
<name>name</name>
<!-- true It means required -->
<required>true</required>
<!-- true Support dynamic values , You can fill in the value jsp expression 、EL expression ,false Do not support -->
<rtexprvalue>true</rtexprvalue>
</attribute>
</tag>
</taglib>Be careful : In defining tag-class The full class name must be used in . When defining attribute names Must match the properties in the helper class
Introduce... On the page JSP label
2.3 stay JSP adopt taglib Instruction import label Library , And access custom tags by specifying suffixes
<%@page import="java.util.Arrays"%>
<%@page import="java.util.List"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@taglib prefix="z" uri="/zking" %>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<%
List<String> lst = Arrays.asList(new String[]{"zs","ls","ww"});
//page->requset->session->application
request.setAttribute("lst", lst);
request.setAttribute("name", "xq");
%>
<!-- requestScope Implicit scope -->
<c:forEach items="${lst }" var="n">
${n }
</c:forEach>
<h2>1. Customize test label </h2>
<z:test name="${name }">888</z:test>
1234<br/>
1213223<br/>
123131313<br/>
231424242424<br/>
</body>
</html>Be careful : Introduce self-defined in the interface JSP When labeling , The path must be consistent with the path you define
summary : stay jsp On the page , We used custom jsp label . It will first pass through the path you introduced uri Find your own tld file . stay tld Find the tag assistant class through the tag name you introduced in the file . In my example , I am here jsp The page uses out label . He first found out label , And then in out Pass under label tag-class Path to find my helper class . In the helper class, pass Java Reflection to invoke your set Method , And then it starts calling doStartTag Method , stay dostartTag Start processing your business in . In my example, I only wrote one dostartTag Method , Why only write one ? Because I inherited BodTagsupprt Of , It has been realized in this dostartTag,doAFterBody,doEndTag Method . I wrote it dostartTag Method just overrides the method of the parent class . And all I have to do is output , It's an empty label , There is no need to write the other two methods
SKIP_BODY: Skip body
EVAL_BODY_INCLUDE: Calculate the main content of the label and [ Output ]
EVAL_PAGE: Calculate subsequent parts of the page
SKIP_PAGE: Skip the rest of the page
EVAL_BODY_AGAIN: Calculate the subject again
边栏推荐
- 2022 safety officer-b certificate theoretical question bank and simulation test
- 一文读懂Logstash原理
- 愉快无负担的跨进程通信方式
- Solr之基礎講解入門
- 抗原產品進入家庭,中國醫療器械企業迎來新藍海
- swiper
- (dp+ group backpack) acwing 9 Group knapsack problem
- DOM知識點總結
- 2022 operation of simulation examination platform for safety officer C certificate
- Summary of DOM knowledge points
猜你喜欢

Jenkins basic configuration

(dp+ group backpack) acwing 9 Group knapsack problem

(digital statistics dp+good) acwing 338 Counting problem

2022 operation of simulation examination platform for safety officer C certificate

明德扬ADC系列开发板-Ad9653子板 多通道 高分辨率 高采样率

多路查找树

解决IDEA下载插件慢的问题
![[naturallanguageprocessing] [multimodal] albef: visual language representation learning based on momentum distillation](/img/b7/0ccd11bd97aa0e217775b31fff3511.jpg)
[naturallanguageprocessing] [multimodal] albef: visual language representation learning based on momentum distillation

2022 high place installation, maintenance and removal of simulated examination platform for operation certificate examination question bank

Fonctionnement de la plate - forme d'examen de simulation pour les agents de sécurité - Questions d'examen de certificat a en 2022
随机推荐
HMS core shows the latest open capabilities in mwc2022, helping developers build high-quality applications
Unity3d C # development of wechat games audio / sound playback problem solving process sharing
Lake Shore—SuperTran-VP 连续流低温恒温器系统
Summary of DOM knowledge points
帝国理工等最新《胶囊网络综述》论文,29页pdf阐述胶囊的概念、方法与应用
CD process
2022 safety officer-b certificate theoretical question bank and simulation test
CD流程
PHP mkdir(): permission denied uploading a file will change the folder permission to 411 permission
Here we go! Dragon lizard community enters PKU classroom
通过财报读懂企业的23个步骤
图及图的遍历
(interval DP | dfs+ memory) acwing 282 Stone merging
一文读懂Logstash原理
Acwing's first question solution attracted the first fan!!! Happy~~~
Handwritten simple promise
sonarqube介绍和安装步骤
Beginner JS BOM implementation window centered
Integrate工具之Jenkins
明德扬ADC系列开发板-Ad9653子板 多通道 高分辨率 高采样率