JavaWeb summary twenty-four, jsp development of traditional labels

A, API labeling technology

API class hierarchy 1.1, labeling technology

  

Second, the API label brief

2.1, JspTag Interface

  JspTag interface is a custom interface for all parent tags , it is a marker interface JSP2.0 newly defined, no attributes and methods . JspTag Tag and SimpleTag interface has two direct child, JSP2.0 previous versions only Tag interface, to implement the Tag interface, custom labels, also known as the traditional labels , to achieve SimpleTag interface custom label is called simple label .

2.2, Tag Interface

  Tag interface is the parent interface for all conventional labels, which defines two important methods (doStartTag, doEndTag) methods and four constants (EVAL_BODY_INCLUDE, to SKIP_BODY, EVAL_PAGE, SKIP_PAGE) , the role of these two methods and four constants are as follows:

  (1) WEB container in the process of interpreting and executing JSP page, met doStartTag method since it will mark custom label to call label processor, doStartTag after the implementation of the method can return to the WEB SKIP_BODY constant EVAL_BODY_INCLUDE or container. If the method returns doStartTag EVAL_BODY_INCLUDE, WEB container will then perform custom label tag body; doStartTag if the method returns SKIP_BODY, WEB ignores the container body defined tag from the tag, the end tag interpreted directly custom label.

  (2) from the WEB container interpreted to define the end of the tag label, the label will call processor doEndTag method, after the implementation of the method doEndTag EVAL_PAGE constant or may return to the WEB SKIP_PAGE vessel. If the method returns doEndTag constant EVAL_PAGE, WEB container will then executes the JSP page is located behind the end mark JSP code; doEndTag if the method returns SKIP_PAGE, WEB pages JSP container ignores all content located behind the end tag.

  As can be seen from the effect doEndTag doStartTag and methods of action and a return value, the function may be implemented in a specific way to write doStartTag doEndTag vivo methods and Java code suitable developing custom labels, and the control method doStartTag method returns doEndTag value, WEB container can also tell whether the contents of the tag body from the definition of tag content and JSP pages located behind the tag end custom labels.

2.3, IterationTag Interface

  IterationTag interface extends the Tag interface, and adds a doAfterBody method and a constant EVAL_BODY_AGAIN on the basis of the Tag interface. IterationTag label interface implemented in addition to be completed Tag interface function can be completed, but also able to notify whether the WEB content container tag body is repeatedly performed. For achieving a custom label IterationTag interface, WEB container doAfterBody method of executing the body of the tag since the tag is defined, the processor will invoke tag, doAfterBody method can return to the WEB SKIP_BODY constant EVAL_BODY_AGAIN or container. If the method returns doAfterBody EVAL_BODY_AGAIN, WEB contents of the container will be the body of the tag is repeatedly performed once again, after the implementation of the method followed doAfterBody call, and so forth, until the method returns doAfterBody constant SKIP_BODY, WEB process vessel will start tag and end tag call doEndTag method.

  It is seen, the development of a custom label, by controlling the return value doAfterBody to tell whether the body of the tag is repeatedly executed WEB container contents, so as to achieve the effect of processing cyclic tag body content. For example, a label can be by iterative IterationTag achieve the output interface elements of a collection of all of the elements specified in the tag portion output format.

  In the JSP API also provides a default implementation of the interface class IterationTag TagSupport , when we write a custom tag handler class defined labels can be inherited and extended TagSupport class, which will simplify development compared to implement IterationTag interface.

2.4, BodyTag Interface

  BodyTag interface extends IterationTag interface and added two methods (setBodyContent, doInitBody) and a constant EVAL_BODY_BUFFERED on the basis of IterationTag interface. The label interface implemented BodyTag IterationTag complete addition the interface function can be completed, the label may also modify the content thereof. For achieving a custom label BodyTag interface, doStartTag method label processor may return not only the front or explain constant EVAL_BODY_INCLUDE SKIP_BODY, also returns the constant EVAL_BODY_BUFFERED. If the method returns EVAL_BODY_BUFFERED doStartTag, WEB container creates a dedicated operation results in the capture tag body BodyContent object, and then calls the method setBodyContent tag processor passes to the tag processor BodyContent referenced object, WEB container body is then performed tag BodyContent write result to the object. In the subsequent event tag processor method, an execution result can be obtained by reference to the tag body BodyContent previously saved object and then calls a method on the object-specific BodyContent BodyContent content object (i.e., the execution result of the tag body) to be modified and a control output.

  In the JSP API also provides an implementation class BodyTagSupport BodyTag interface , we can modify the label tag handler class custom tag body content can be inherited and extended BodyTagSupport classes in writing, which will simplify development compared to implement the interface BodyTag .

2.5, SimpleTag Interface

  SimpleTag JSP2.0 in the interface is a new label interface . As the traditional label use three tabs interfaces to perform different functions, appears to be too cumbersome and not conducive to the promotion tag technology, therefore, SUN companies to reduce the learning curve tag technology, defined in a more simple JSP 2.0, the easy to write and calls SimpleTag interface. SimpleTag biggest difference is that the interface with the conventional label interface, doTag SimpleTag interface defines a method for processing tag logic, the method calls the custom tag WEB container performed, and is called once. Those using conventional label interface functions performed, for example, whether the label body, label iteration thereof, to modify the contents of the tag body and other functions can be completed in doTag process.

  In the JSP API also provides the interface implementation class SimpleTagSupport SimpleTag , when we write a simple label, you can be inherited and extended SimpleTagSupport class, which will be achieved than SimpleTag interfaces simplify development.

2.6, each of the conventional tag interface method may return a return value

  FIG lists the Tag interface, IterationTag BodyTag interface, and interfaces the main and description thereof respectively, may return a return value.

  

Third, the development of traditional labels to achieve a logical page

  Developers in the preparation of Jsp page, often also need to introduce some logic in the page, for example:

  • Jsp page control a certain part of the content is executed.
  • Control the entire jsp page is executed.
  • Control jsp page content repeated.
  • Jsp page modify content output.

  Custom tags may be removed except jsp pages java code, but it can also achieve the above functions.

3.1, jsp page control a certain part of the content is executed  

  编写一个类实现tag接口,控制doStartTag()方法的返回值,如果这个方法返回EVAL_BODY_INCLUDE,则执行标签体,如果返回SKIP_BODY,则不执行标签体。

  SUN公司针对tag接口提供了一个默认的实现类TagSupport,TagSupport类中实现了tag接口的所有方法,因此我们可以编写一个类继承TagSupport类,然后再重写doStartTag方法。

示例代码如下:

TagDemo1.java

复制代码
 1 package me.gacl.web.tag;
 2 
 3 import javax.servlet.jsp.JspException;
 4 import javax.servlet.jsp.tagext.Tag;
 5 import javax.servlet.jsp.tagext.TagSupport;
 6 
 7 /**
 8  * @author gacl
 9  * TagSupport类实现了Tag接口,TagDemo1继承TagSupport类
10  * 
11  */
12 public class TagDemo1 extends TagSupport {
13 
14     /* 重写doStartTag方法,控制标签体是否执行
15      * @see javax.servlet.jsp.tagext.TagSupport#doStartTag()
16      */
17     @Override
18     public int doStartTag() throws JspException {
19         //如果这个方法返回EVAL_BODY_INCLUDE,则执行标签体,如果返回SKIP_BODY,则不执行标签体
20         //return Tag.EVAL_BODY_INCLUDE;
21         return Tag.SKIP_BODY;
22     }
23 }
复制代码

  在WEB-INF目录下的tld文件中添加对该标签处理类的描述,如下:

复制代码
1 <tag>
2         <name>demo1</name>
3         <tag-class>me.gacl.web.tag.TagDemo1</tag-class>
4         <!--demo1标签有标签体,所以这里的body-content设置为JSP-->
5         <body-content>JSP</body-content>
6 </tag>
复制代码

  在jsp页面中导入并使用自定义标签,如下:

复制代码
 1 <%@ page language="java" pageEncoding="UTF-8"%>
 2 <%--在jsp页面中导入自定义标签库 --%>
 3 <%@taglib uri="/gacl" prefix="gacl" %>
 4 <!DOCTYPE HTML>
 5 <html>
 6   <head>
 7     <title>控制标签体是否执行</title>
 8   </head>
 9   
10   <body>
11   <%--在jsp页面中使用自定义标签 demo1标签是带有标签体的,标签体的内容是"孤傲苍狼"这几个字符串--%>
12     <gacl:demo1>
13         孤傲苍狼
14     </gacl:demo1>
15   </body>
16 </html>
复制代码

  运行效果如下:

 

3.2、控制整个jsp页面是否执行

  编写一个类实现tag接口,控制doEndTag()方法的返回值,如果这个方法返回EVAL_PAGE,则执行标签余下的jsp页面,如果返回SKIP_PAGE,则不执行余下的jsp。

示例代码如下:

TagDemo2.java

复制代码
 1 package me.gacl.web.tag;
 2 
 3 import javax.servlet.jsp.JspException;
 4 import javax.servlet.jsp.tagext.Tag;
 5 import javax.servlet.jsp.tagext.TagSupport;
 6 
 7 /**
 8  * @author gacl
 9  * TagSupport类实现了Tag接口,TagDemo2继承TagSupport类
10  */
11 public class TagDemo2 extends TagSupport{
12 
13     /* 重写doEndTag方法,控制jsp页面是否执行
14      * @see javax.servlet.jsp.tagext.TagSupport#doEndTag()
15      */
16     @Override
17     public int doEndTag() throws JspException {
18         //如果这个方法返回EVAL_PAGE,则执行标签余下的jsp页面,如果返回SKIP_PAGE,则不执行余下的jsp
19         return Tag.SKIP_PAGE;
20         //return Tag.EVAL_PAGE;
21     }
22 
23     
24 }
复制代码

  在WEB-INF目录下的tld文件中添加对该标签处理类的描述,如下:

复制代码
1 <tag>
2         <name>demo2</name>
3         <tag-class>me.gacl.web.tag.TagDemo2</tag-class>
4         <!--demo2标签没有标签体,所以这里的body-content设置为empty-->
5         <body-content>empty</body-content>
6 </tag>
复制代码

  在jsp页面中导入并使用自定义标签,如下:

复制代码
 1 <%@ page language="java" pageEncoding="UTF-8"%>
 2 <%--在jsp页面中导入自定义标签库 --%>
 3 <%@taglib uri="/gacl" prefix="gacl" %>
 4 <!DOCTYPE HTML>
 5 <html>
 6   <head>
 7     <title>控制jsp页面是否执行</title>
 8   </head>
 9   
10   <body>
11          <h1>jsp页面的内容1</h1>
12          <%--在jsp页面中使用自定义标签 demo2标签是不带标签体的--%>
13          <gacl:demo2/>
14          <h1>jsp页面的内容2</h1>
15   </body>
16 </html>
复制代码

  运行效果如下:

 

3.3、控制jsp页面内容重复执行

  编写一个类实现Iterationtag接口,控制doAfterBody()方法的返回值,如果这个方法返回EVAL_BODY_AGAIN, 则web服务器又执行一次标签体,依次类推,一直执行到doAfterBody方法返回SKIP_BODY,则标签体才不会重复执行。

示例代码如下:

TagDemo3.java

复制代码
 1 package me.gacl.web.tag;
 2 
 3 import javax.servlet.jsp.JspException;
 4 import javax.servlet.jsp.tagext.IterationTag;
 5 import javax.servlet.jsp.tagext.Tag;
 6 import javax.servlet.jsp.tagext.TagSupport;
 7 
 8 public class TagDemo3 extends TagSupport {
 9 
10     int x = 5;
11     @Override
12     public int doStartTag() throws JspException {
13         return Tag.EVAL_BODY_INCLUDE;
14     }
15     
16     /* 控制doAfterBody()方法的返回值,
17      * 如果这个方法返回EVAL_BODY_AGAIN, 则web服务器又执行一次标签体,
18      * 依次类推,一直执行到doAfterBody方法返回SKIP_BODY,则标签体才不会重复执行。
19      * @see javax.servlet.jsp.tagext.TagSupport#doAfterBody()
20      */
21     @Override
22     public int doAfterBody() throws JspException {
23         x--;
24         if(x>0){
25             return IterationTag.EVAL_BODY_AGAIN;
26         }else{
27             return IterationTag.SKIP_BODY;
28         }
29     }
30 
31 }
复制代码

  在WEB-INF目录下的tld文件中添加对该标签处理类的描述,如下:

复制代码
1 <tag>
2         <name>demo3</name>
3         <tag-class>me.gacl.web.tag.TagDemo3</tag-class>
4         <!--demo3标签有标签体,所以这里的body-content设置为JSP-->
5         <body-content>JSP</body-content>
6 </tag>
复制代码

  在jsp页面中导入并使用自定义标签,如下:

复制代码
 1 <%@ page language="java" pageEncoding="UTF-8"%>
 2 <%--在jsp页面中导入自定义标签库 --%>
 3 <%@taglib uri="/gacl" prefix="gacl" %>
 4 <!DOCTYPE HTML>
 5 <html>
 6   <head>
 7     <title>控制页面内容重复执行5次</title>
 8   </head>
 9   
10   <body>
11   <%--在jsp页面中使用自定义标签 demo3标签--%>
12       <gacl:demo3>
13           <h3>jsp页面的内容</h3>
14       </gacl:demo3>
15   </body>
16 </html>
复制代码

  运行效果如下:

  

3.4、修改jsp页面内容输出

  编写一个类实现BodyTag接口,控制doStartTag()方法返回EVAL_BODY_BUFFERED,则web服务器会创建BodyContent对象捕获标签体,然后在doEndTag()方法体内,得到代表标签体的bodyContent对象,从而就可以对标签体进行修改操作。

  SUN公司针对BodyTag接口提供了一个默认的实现类BodyTagSupport,BodyTagSupport类中实现了BodyTag接口的所有方法,因此我们可以编写一个类继承BodyTagSupport类,然后再根据需要重写doStartTag方法和doEndTag()方法。

示例代码如下:

TagDemo4.java

复制代码
 1 package me.gacl.web.tag;
 2 
 3 import java.io.IOException;
 4 
 5 import javax.servlet.jsp.JspException;
 6 import javax.servlet.jsp.tagext.BodyContent;
 7 import javax.servlet.jsp.tagext.BodyTag;
 8 import javax.servlet.jsp.tagext.BodyTagSupport;
 9 import javax.servlet.jsp.tagext.Tag;
10 
11 /**
12  * @author gacl
13  * BodyTagSupport类实现了BodyTag接口接口,TagDemo4继承 BodyTagSupport类
14  */
15 public class TagDemo4 extends BodyTagSupport {
16 
17     /* 控制doStartTag()方法返回EVAL_BODY_BUFFERED
18      * @see javax.servlet.jsp.tagext.BodyTagSupport#doStartTag()
19      */
20     @Override
21     public int doStartTag() throws JspException {
22         return BodyTag.EVAL_BODY_BUFFERED;
23     }
24     
25     @Override
26     public int doEndTag() throws JspException {
27         
28         //this.getBodyContent()得到代表标签体的bodyContent对象
29         BodyContent bodyContent = this.getBodyContent();
30         //拿到标签体
31         String content = bodyContent.getString();
32         //修改标签体里面的内容,将标签体的内容转换成大写
33         String result = content.toUpperCase();
34         try {
35             //输出修改后的内容
36             this.pageContext.getOut().write(result);
37         } catch (IOException e) {
38             throw new RuntimeException(e);
39         }
40         
41         return Tag.EVAL_PAGE;
42     }
43 }
复制代码

  在WEB-INF目录下的tld文件中添加对该标签处理类的描述,如下:

复制代码
1 <tag>
2         <name>demo4</name>
3         <tag-class>me.gacl.web.tag.TagDemo4</tag-class>
4         <!--demo4标签有标签体,所以这里的body-content设置为JSP-->
5         <body-content>JSP</body-content>
6 </tag>
复制代码

  在jsp页面中导入并使用自定义标签,如下:

复制代码
 1 <%@ page language="java" pageEncoding="UTF-8"%>
 2 <%--在jsp页面中导入自定义标签库 --%>
 3 <%@taglib uri="/gacl" prefix="gacl" %>
 4 <!DOCTYPE HTML>
 5 <html>
 6   <head>
 7     <title>修改jsp页面内容输出</title>
 8   </head>
 9   
10   <body>
11   <%--在jsp页面中使用自定义标签 demo4标签--%>
12       <gacl:demo4>
13           <h3>xdp_gacl</h3>
14       </gacl:demo4>
15   </body>
16 </html>
复制代码

  运行效果如下:

  

四、jsp传统标签开发总结

  在现在的jsp标签开发中,很少直接使用传统标签来开发了,目前用得较多的都是简单标签,所以Jsp的传统标签开发了解一下即可,下一篇重点介绍jsp简单标签的开发

发布了29 篇原创文章 · 获赞 3 · 访问量 2万+

Guess you like

Origin blog.csdn.net/yangleiGJ/article/details/54407472