Case Study of JSP Custom Tag

  • 2021-10-13 08:18:12
  • OfStack

This article introduces the case of JSP custom label for your reference, the specific contents are as follows

Case 1: Implement a basic anti-theft chain label

1. Label Handling Class


public class MyReferer extends BodyTagSupport {
  private String site;
  private String back;
  public String getSite() {
    return site;
  }
  public void setSite(String site) {
    this.site = site;
  }
  public String getBack() {
    return back;
  }
  public void setBack(String back) {
    this.back = back;
  }
  public int doEndTag() throws JspException {
    //  Get JSP Context environment object 
    PageContext pageContext = this.pageContext;
    //  Get to request Object 
    HttpServletRequest request = (HttpServletRequest)pageContext.getRequest();
    //  Judge 
    String header = request.getHeader("referer");
    if(header != null && header.startsWith(getSite())){
      //  Execute subsequent pages 
      return Tag.EVAL_PAGE;
    }else{
      //  Redirection of pages 
      HttpServletResponse response = (HttpServletResponse)pageContext.getResponse();
      try {
        response.sendRedirect(getBack());
      } catch (IOException e) {
        e.printStackTrace();
      }
      //  Do not execute 
      return Tag.SKIP_PAGE;
    }
  }
}

2. Describe the file


<?xml version="1.0" encoding="UTF-8"?>
<taglib 
  xmlns="http://java.sun.com/xml/ns/javaee"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-jsptaglibrary_2_1.xsd"
  version="2.1">
 <!-- 2.  Writing Tag Library Description Files  --> 
 <tlib-version>1.0</tlib-version>
 <short-name>jnb</short-name>
  <tag> 
  <name>referer</name>
  <tag-class>cn.itcast.custom.MyReferer</tag-class>
  <body-content>empty</body-content>
   <attribute>
    <name>site</name>
    <required>true</required>
    <rtexprvalue>true</rtexprvalue>
  </attribute>
   <attribute>
    <name>back</name>
    <required>true</required>
    <rtexprvalue>true</rtexprvalue>
  </attribute>
 </tag>
</taglib> 

3. Introduction and use


<%@taglib uri="/WEB-INF/referer.tld" prefix="my"%>
  <my:referer site=http://localhost:8080/day11/list.jsp
 back="/day11/list.jsp"/>

JSP 2.0 Custom Label

--SimpleTag interface

Defines the lifecycle method of the label handling class. doTag ()

--SimpleTagSupport category

All the methods of the SimpleTag interface are implemented, so we just need to inherit and override the class later.

Case 2: Implement your own if …. else tag

Objectives:


 <%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
  <c:choose>
   <c:when test="<%= 12>1 %>">
       Greater than 
   </c:when>
   <c:otherwise>
       Less than 
   </c:otherwise>
  </c:choose>

Analysis:

1. ChooseTag. java, 1 tag field attribute must be defined


public class ChooseTag extends SimpleTagSupport {
  private boolean tag = true;
  public boolean isTag() {
    return tag;
  }
  public void setTag(boolean tag) {
    this.tag = tag;
  }
  //  Automatically execute when encountering labels 
  public void doTag() throws JspException, IOException {
    //  Get the tag body object 
    JspFragment body = this.getJspBody();
    //  Execute tag body 
    body.invoke(null);
    super.doTag();
  }
}

2. WhenTag.java


public class WhenTag extends SimpleTagSupport {
  private boolean test;
  public boolean isTest() {
    return test;
  }
  public void setTest(boolean test) {
    this.test = test;
  }
  //  Automatically execute when encountering labels 
  public void doTag() throws JspException, IOException {
    //  Get the parent element 
    ChooseTag choose = (ChooseTag)this.getParent();
    //  Gets the tag variable value of the parent element 
    boolean parent = choose.isTag();
    //  Judge 
    if( parent && this.isTest() ){
      //  Execute tag body 
      JspFragment body = this.getJspBody();
      body.invoke(null);
    }
    super.doTag();
  }
}

3. Otherwise.java


public class OtherwiseTag extends SimpleTagSupport {
  
  //  Automatically execute when encountering labels 
  public void doTag() throws JspException, IOException {
    //  Get the parent element 
    ChooseTag choose = (ChooseTag)this.getParent();
    //  Gets the tag variable value of the parent element 
    boolean parent = choose.isTag();
    //  Judge 
    if(parent){
      //  Execute tag body 
      JspFragment body = this.getJspBody();
      body.invoke(null);
    }
    super.doTag();
  }
}

4. Describe the file


<?xml version="1.0" encoding="UTF-8"?>
<taglib 
  xmlns="http://java.sun.com/xml/ns/javaee"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-jsptaglibrary_2_1.xsd"
  version="2.1">
 <!-- 2.  Writing Tag Library Description Files  --> 
 <tlib-version>1.0</tlib-version>
 <short-name>jnb</short-name>
  <tag> 
  <name>choose</name>
  <tag-class>cn.itcast.tags.ChooseTag</tag-class>
  <body-content>scriptless</body-content>   JSP2.0 Mode 
 </tag>
 <tag> 
  <name>when</name>
  <tag-class>cn.itcast.tags.WhenTag</tag-class>
  <body-content>scriptless</body-content>
  <attribute>
    <name>test</name>
    <required>true</required>
    <rtexprvalue>true</rtexprvalue>
  </attribute>
 </tag>
  
  <tag> 
  <name>otherwise</name>
  <tag-class>cn.itcast.tags.OtherwiseTag</tag-class>
  <body-content>scriptless</body-content>
 </tag>
</taglib>

5. Introduction and use


<%@taglib uri="/WEB-INF/ifelse.tld" prefix="jnb"%>
   <jnb:choose>
    <jnb:when test="<%= 1>2 %>">
         Less than 
    </jnb:when>
    <jnb:otherwise>
          Greater than 
    </jnb:otherwise>
   </jnb:choose> 

Packaging Custom Tag Library

1. Create an taglibs folder

2. Copy all the class files corresponding to the tag handling classes together with the package to the directory in 1

3. Create an META-INF folder in the folder in 1

4. Copy the tld file to the META-INF directory

5. Edit the tld file to introduce the uri element: < uri > http://www.jnb.com < /uri > Provide the incoming url path

6. Packaging with the jar command: D:\ mytaglibs > jar cvf jnb.jar *

Summarize

Master how to use JSP 2.0 custom label development and packaging.

1. Create an taglibs folder

2. Copy all the class files corresponding to the tag-handling classes together with the package to the directory in 1

3. Create an META-INF folder in the folder in 1

4. Copy the tld file to the META-INF directory

5. Edit the tld file to introduce the uri element: < uri > http://www.jnb.com < /uri > Provide the incoming url path

6. Packaging with the jar command: D:\ mytaglibs > jar cvf jnb.jar *

Summarize

Master how to use JSP 2.0 custom label development and packaging.


Related articles: