JSTL

 【概述】

Jsp standard tag library的缩写,简称为Jsp标准标签库。Jstl不是用于替代El表达式,Jstl是对El表达式的功能进行补充。

 

【基本标签】

1)       <c:out>标签

  • 作用

              显示(输出)数据对象或表达式的结果。

  • 属性

-- value           :待输出内容(常量、表达式)

--escapeXml :是否忽略标签的原本含义。true表示忽略。表将将作为一般文本内容进行显示。否则将正常解释。

 

2)       <c:set>标签

  • 作用

设置作用域的属性值。相当于java的setAttribute()。

  • 属性

--var               :作用域属性的名称

-- value           :作用域属性的值

--scope          :作用域的范围,application、session、request、page

 

3)       <c:remove>标签

  • 作用

移出指定作用域中的属性值。相当于removeAttribute()方法

  • 属性

--var               :待删除属性的名称

--scope          :属性的作用域范围

 

4)       <c:if>标签

  • 作用

              相当于Java中的if语句,起到逻辑判断功能。

  • 属性

--test              :执行if标签的条件判断表达式

--var               :表达式存储在作用域中的属性名

 

5)       <c:choose>标签(一级标签)

  • 作用

              多分支条件语句。相当于Java中的switch语句。

 

6)       <c:when>标签(二级标签)

  • 作用

       执行分支条件判断。相当于case语句。只能用在<c:choose>标签内

  • 属性

--test              :执行逻辑判断。

 

7)       <c:otherwise>标签(二级标签)

  • 作用

              相当于default语句。只能用在<c:choose>标签中。

 

8)       <c:forEach>标签

  • 作用

              控制循环次数或遍历集合中元素。

  • 属性

--begin           :循环变量的(初值)起始值

--end              :循环变量的终止值

--step             :循环变量的增长速度(步长)

--var               :作用域中属性的名称

--itmes           :待遍历的集合对象

 

【自定义函数】

1)       创建.tld文件

 

2)       复制标准标签库中函数的描述文件内容并粘贴到自定义的tld文件中

 

3) 修改描述文件(自定义的.tld文件)

 

4) 编写对应的Java方法

 1 public class ShowInfo {
 2     /**
 3      * 自定义函数的要求
 4      *     1、方法需要使用public修饰符进行修饰
 5      *     2、方法必须为静态方法
 6      * @param name
 7      * @return
 8      */
 9     public static String show(String name){
10         return "Hello, " + name;
11     }
12 }

 

5) 应用自定义函数

1   <body>
2       ${myFn:show("mike")}
3       
4   </body>

 

6) 执行结果

 

 

【自定义标签】

1)       创建Tld文件

2)       复制标准标签库描述文件的内容到自定义Tld文件中

3)       配置自定义标签

 1 <taglib xmlns="http://java.sun.com/xml/ns/javaee"
 2     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 3     xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-jsptaglibrary_2_1.xsd"
 4     version="2.1">
 5     
 6   <description>customer tag library</description>
 7   <display-name>customer tag</display-name>
 8   <tlib-version>1.0</tlib-version>
 9   <short-name>t</short-name>
10   <uri>http://hl/tag/mytag</uri>
11 
12   <validator>
13     <description>
14         Provides core validation features for JSTL tags.
15     </description>
16     <validator-class>
17         org.apache.taglibs.standard.tlv.JstlCoreTLV
18     </validator-class>
19   </validator>
20 
21 
22   <tag>
23     <description>
24         this is tag description
25     </description>
26     <!-- 自定义标签名称 -->
27     <name>showText</name>
28     <!-- 标签所对应的Java类 -->
29     <tag-class>cn.hl.tag.ShowInfo</tag-class>
30     <!-- 标签内容的类型 -->
31     <body-content>JSP</body-content>
32     <!-- 标签的属性,对应于Java代码的字段+set访问器 -->
33     <attribute>
34         <description>
35             user's name
36         </description>
37         <!-- 属性名 -->
38         <name>name</name>
39         <!-- 是否为必须提供的属性 -->
40         <required>true</required>
41         <!-- 是否支持Jsp表达式 -->
42         <rtexprvalue>true</rtexprvalue>
43     </attribute> 
44     <attribute>
45         <name>other</name>
46         <required>false</required>
47         <rtexprvalue>true</rtexprvalue>
48     </attribute>   
49   </tag>
50 </taglib>

 

4) 编写对应的处理类

 1 /**
 2  * 需要继承于TagSupport
 3  * @author Terry
 4  *
 5  */
 6 public class ShowInfo extends TagSupport{
 7     //属性,对应于配置文件的attribute
 8     private String name;
 9     private boolean other;
10     
11     public void setName(String name) {
12         this.name = name;
13     }
14     
15     public void setOther(boolean other){
16         this.other = other;
17     }
18 
19 
20     /**
21      * 控制标签后的内容的显示方式
22      *     EVAL_PAGE        :正常显示标签后的内容
23      *     SKIP_PAGE        :忽略标签后内容的显示
24      */
25     @Override
26     public int doEndTag() throws JspException {
27         System.out.println("do End Tag......");
28         
29         if(other){
30             return EVAL_PAGE;
31         }
32         else{
33             return SKIP_PAGE;
34         }
35     }
36 
37     /**
38      * 控制标签中的内容的显示方式
39      *     EVAL_BODY_INCLUDE    :包含标签的内容(将标签中间内容添加到输出流中)
40      *     SKIP_BODY            :忽略标签的内容(不进行输出)
41      */
42     @Override
43     public int doStartTag() throws JspException {
44         System.out.println("do start Tag......");
45         
46         if("baidu".equals(name))
47             return EVAL_BODY_INCLUDE;
48         else
49             return SKIP_BODY; 
50         
51     }
52     
53 }

 

5)测试

 1       <t:showText name="Mike" other="true">
 2           <a href="http://baidu.com">baidu</a>
 3       </t:showText>
 4       <br />
 5       <t:showText name="baidu" other="true">
 6           <a href="http://google.com">google</a>
 7       </t:showText>
 8       <br />
 9       
10       <hr />
11       自定义标签测试结束

 

posted @ 2018-10-23 19:17  猩生柯北  阅读(358)  评论(0)    收藏  举报