[原创]java WEB学习笔记41:简单标签之带属性的自定义标签(输出指定文件,计算并输出两个数的最大值 demo)

本博客为原创:综合 尚硅谷(http://www.atguigu.com)的系统教程(深表感谢)和 网络上的现有资源(博客,文档,图书等),资源的出处我会标明

本博客的目的:①总结自己的学习过程,相当于学习笔记 ②将自己的经验分享给大家,相互学习,互相交流,不可商用

内容难免出现问题,欢迎指正,交流,探讨,可以留言,也可以通过以下方式联系。

本人互联网技术爱好者,互联网技术发烧友

微博:伊直都在0221

QQ:951226918

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

1. 带属性的自定义标签

  1)先在标签处理器类中定义 setter 方法:建议把所有的属性的类型都设置成 String 类型

 1 private String value;
 2     private String count;
 3     
 4     public void setValue(String value) {
 5         this.value = value;
 6     }
 7     
 8     public void setCount(String count) {
 9         this.count = count;
10     }

 

   2)在 tld 描述文件中来描述属性

 1 <!-- 描述当前的标签的属性  -->
 2       <attribute>
 3           <!-- 属性名  需要和标签处理类的setter 方法定义的属性相同-->
 4           <name>value</name>
 5           <!-- 该属性名是必须的 -->
 6           <required>true</required>
 7           <!-- rtexprvalue :runtime expression value  当前属性是否可以运行时表达式的动态值-->
 8           <rtexprvalue>true</rtexprvalue>
 9       </attribute>
10       
11       <attribute>
12           <!-- 属性名 -->
13           <name>count</name>
14           <!-- 该属性名是必须的 -->
15           <required>false</required>
16           <!-- rtexprvalue :runtime expression value  当前属性是否可以运行时表达式的动态值-->
17           <rtexprvalue>false</rtexprvalue>
18       </attribute>

  3)在页面中使用标签,并且加入属性,属性名同 tld 文件中定义的名字

    

1 <jason:hell value="${param.name }" count="10"/>

2.练习

  demo1. 定制一个带有两个属性的标签<max>, 用于计算并输出两个数的最大值

  MaxTag.java

    

 1 package com.jason.tag.mytag;
 2 
 3 import java.io.IOException;
 4 
 5 import javax.servlet.jsp.JspContext;
 6 import javax.servlet.jsp.JspException;
 7 import javax.servlet.jsp.JspWriter;
 8 import javax.servlet.jsp.PageContext;
 9 import javax.servlet.jsp.tagext.JspFragment;
10 import javax.servlet.jsp.tagext.JspTag;
11 import javax.servlet.jsp.tagext.SimpleTag;
12 
13 public class MaxTag implements SimpleTag {
14 
15     private PageContext pageContext;
16     private String num1;
17     private String num2;
18 
19     public void setNum1(String num1) {
20         this.num1 = num1;
21     }
22 
23     public void setNum2(String num2) {
24         this.num2 = num2;
25     }
26 
27     @Override
28     public void doTag() throws JspException, IOException {
29         int b = 0; 
30         int a = 0;
31 
32         JspWriter out = pageContext.getOut();
33         try {
34             a = Integer.parseInt(num1);
35             b = Integer.parseInt(num2);
36             
37             out.print(a > b ? a : b);
38         } catch (Exception e) {
39             out.print("输入有问题");
40         }
41 
42     }
43 
44     @Override
45     public JspTag getParent() {
46         // TODO Auto-generated method stub
47         return null;
48     }
49 
50     @Override
51     public void setJspBody(JspFragment arg0) {
52         // TODO Auto-generated method stub
53 
54     }
55 
56     @Override
57     public void setJspContext(JspContext pageContext) {
58         this.pageContext = (PageContext) pageContext;
59 
60     }
61 
62     @Override
63     public void setParent(JspTag arg0) {
64         
65     }
66 
67 }

  mytag.tld

 1 <?xml version="1.0" encoding="UTF-8" ?>
 2 
 3 
 4 <taglib xmlns="http://java.sun.com/xml/ns/j2ee"
 5     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 6     xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-jsptaglibrary_2_0.xsd"
 7     version="2.0">
 8     
 9   <!-- 描述 tld 文件 -->
10   <description>jaosn's first DIY  jsp tag</description>
11   <display-name>jason tag</display-name>
12   <tlib-version>1.0</tlib-version>
13   
14   <!-- 建议在 JSP 页面上使用的标签的前缀  -->
15   <short-name>jason</short-name>
16   
17   <!--  作为 tld 文件的 id ,用来唯一标识当前的 tld 文件,多个 tld 文件的 uri 不能重复,通过 JSP 页面的 taglib 指令的 uri 属性引用 -->
18   <uri>http://www.jason.com/jason/diy/tag/core</uri>
19   
20   
21   <tag>
22       <name>max</name>
23       <tag-class>com.jason.tag.mytag.MaxTag</tag-class>
24       <body-content>empty</body-content>
25       
26       <attribute>
27           <name>num1</name>
28           <required>true</required>
29           <rtexprvalue>true</rtexprvalue>
30       </attribute>
31       
32       <attribute>
33           <name>num2</name>
34           <required>true</required>
35           <rtexprvalue>true</rtexprvalue>
36       </attribute>
37       
38   </tag>

  testmytag.jsp

1 <body>
2     
3     <jason:max num2="${param.a }" num1="${param.b }"/>
4 
5 </body>

注意:通常在开发中,将标签处理类 继承 SimpleTagSupport 类,更加方便,简洁。 直接调用对应的getter方法 得到对应的 API

 1 package javax.servlet.jsp.tagext;
 2 
 3 import java.io.IOException;
 4 
 5 import javax.servlet.jsp.JspContext;
 6 import javax.servlet.jsp.JspException;
 7 
 8 
 9 public class SimpleTagSupport implements SimpleTag {
10 
11     public SimpleTagSupport() {
12     }
13     
14     public void doTag() throws JspException, IOException {  
15     }
16     
17     private JspTag parentTag; 
18     public void setParent( JspTag parent ) {
19         this.parentTag = parent;
20     }
21     
22     public JspTag getParent() {
23         return this.parentTag;
24     }
25     
26     
27     private JspContext jspContext;
28     public void setJspContext( JspContext pc ) {
29         this.jspContext = pc;
30     }
31     
32     protected JspContext getJspContext() {
33         return this.jspContext;
34     }
35                 
36     
37     private JspFragment jspBody;
38     public void setJspBody( JspFragment jspBody ) {
39         this.jspBody = jspBody;
40     }
41     
42     protected JspFragment getJspBody() {
43         return this.jspBody;
44     }
45 
46    
47     public static final JspTag findAncestorWithClass(
48         JspTag from, Class<?> klass) 
49     {
50         boolean isInterface = false;
51 
52         if (from == null || klass == null
53                 || (!JspTag.class.isAssignableFrom(klass)
54                     && !(isInterface = klass.isInterface()))) {
55             return null;
56         }
57 
58         for (;;) {
59             JspTag parent = null;
60             if( from instanceof SimpleTag ) {
61                 parent = ((SimpleTag)from).getParent();
62             }
63             else if( from instanceof Tag ) {
64                 parent = ((Tag)from).getParent();
65             }
66             if (parent == null) {
67                 return null;
68             }
69 
70             if (parent instanceof TagAdapter) {
71                 parent = ((TagAdapter) parent).getAdaptee();
72             }
73 
74             if ((isInterface && klass.isInstance(parent))
75                     || klass.isAssignableFrom(parent.getClass())) {
76                 return parent;
77             }
78 
79             from = parent;
80         }
81     }    
82 }

  demo2.定制一个带有一个属性的标签<atgiugu: readFile src=“”>, 用于输出指定文件的

   ReadFileTag.java

 1 package com.jason.tag.mytag;
 2 
 3 import java.io.BufferedReader;
 4 import java.io.IOException;
 5 import java.io.InputStream;
 6 import java.io.InputStreamReader;
 7 
 8 import javax.servlet.jsp.JspException;
 9 import javax.servlet.jsp.PageContext;
10 import javax.servlet.jsp.tagext.SimpleTagSupport;
11 
12 public class ReadFileTag extends SimpleTagSupport{
13     
14     //相对于当前 web应用的根路径的文件名
15     private String src;
16     
17     public void setSrc(String src) {
18         this.src = src;
19     }
20     
21     @Override
22     public void doTag() throws JspException, IOException {
23         PageContext pageContext = (PageContext) getJspContext();
24         InputStream in  = pageContext.getServletContext().getResourceAsStream(src);
25         
26         BufferedReader br = new BufferedReader(new InputStreamReader(in));
27         String str = null;
28         while((str = br.readLine()) != null){
29             pageContext.getOut().write(str);
30             pageContext.getOut().write("<br>");
31             
32         }
33                 
34         
35         
36         
37     }
38 }

  mytag.tld

 1 <?xml version="1.0" encoding="UTF-8" ?>
 2 
 3 
 4 <taglib xmlns="http://java.sun.com/xml/ns/j2ee"
 5     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 6     xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-jsptaglibrary_2_0.xsd"
 7     version="2.0">
 8     
 9   <!-- 描述 tld 文件 -->
10   <description>jaosn's first DIY  jsp tag</description>
11   <display-name>jason tag</display-name>
12   <tlib-version>1.0</tlib-version>
13   
14   <!-- 建议在 JSP 页面上使用的标签的前缀  -->
15   <short-name>jason</short-name>
16   
17   <!--  作为 tld 文件的 id ,用来唯一标识当前的 tld 文件,多个 tld 文件的 uri 不能重复,通过 JSP 页面的 taglib 指令的 uri 属性引用 -->
18   <uri>http://www.jason.com/jason/diy/tag/core</uri>
19   
20   <tag>
21       <name>readFile</name>
22       <tag-class>com.jason.tag.mytag.ReadFileTag</tag-class>
23       <body-content>empty</body-content>
24       
25       <attribute>
26           <name>src</name>
27           <required>true</required>
28           <rtexprvalue>true</rtexprvalue>
29       </attribute>
30   </tag>

  testmytag.jsp

1 <jason:readFile src="/note.txt"/>
原文地址:https://www.cnblogs.com/jasonHome/p/5559196.html