Custom tags based on JSP are introduced using examples

  • 2020-05-26 09:55:50
  • OfStack

Add JSP custom tags:

Start by adding an tld file to the WEB-INF folder
< ?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" >

< description > myTag < /description >
< display-name > JSTL core < /display-name >
< tlib-version > 1.0 < /tlib-version >
< short-name > cnweb < /short-name >
< uri > http://www.cnweb.cn < /uri >

< tag > < ! -- one tag tag corresponds to one custom tag tag class -- >
< description > MyTag < /description >
< name > when < /name >
< tag-class > cn.example.when < /tag-class >
< body-content > scriptless < /body-content >
< attribute >
< name > test < /name >
< required > true < /required >
< rtexprvalue > true < /rtexprvalue >
< /attribute >
< /tag >
< /taglib >
Reference rules:
< %@ taglib uri="http://www.cnweb.cn" prefix="cnweb"% >

Define the error handling page:

< error-page >
< exception-type > java.lang.Exception < /exception-type >
< location > /errors/error.jsp < /location >
< /error-page >
< error-page >
< error-code > 404 < /error-code >
< location > /errors/error1.jsp < /location >
< /error-page >

Custom label execution process:

SimpleTagSupport

/* Jsp engine encounters a simple class and instantiates it;
* call setJspContext, pass the page pageContext to the tag handling class;
* call setParent to pass in the parent; if not, pass null
* calls the setJspBody method, passing the wrapped tag body JspFragment to the tag handler class
* execute the custom tag on the page, doTag() method; -- > When execution is complete, the object is destroyed
*/

JspFragment jf = this.getJspBody();
jf. invoke (this. getJspContext () getOut ()); // if it is not displayed, it is not processed

--------------------------------------------------------------------

public class tagShowOrNot extends TagSupport {
public int doStartTag() throws JspException {
return Tag. EVAL_BODY_INCLUDE; / / display body
/ / return Tag. SKIP_BODY; / / hide body
/ / Tag. EVAL_PAGE; / / display page
/ / Tag. SKIP_PAGE; / / hide page
}}

TagSupport
/* write a class that implements TagSupport;
* label processor class is described in tld file (location of tld file: WEB-INF)
* import and use tags on the jsp page
*
* when a custom tag is encountered during the execution of jsp, the class is instantiated first;
* then execute method: setPageContext()-- > setParent()-- > doStartTag()
* if there is a tag body, 1 will generally execute the tag body; doEndTag () - > After the entire tag is executed, 1 will normally be executed :release()
*
* controls whether the entire jsp page is executed;
* controls whether part 1 of the jsp page is executed;
* control the repeated execution of jsp page contents;
* modify jsp page content output;
*/

// this method executes after the tag body, but before doEndTag(), until the method returns IterationTag.SKIP_BODY;
public int doAfterBody() throws JspException {
time--;
System.out.println (" repeat "+ time);
if (time > 0) {
return IterationTag.EVAL_BODY_AGAIN;
} else
return IterationTag.SKIP_BODY;
}


Related articles: