当前位置:网站首页>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

原网站

版权声明
本文为[ˡ A cup of American style]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/162/202206112346061445.html