1. 定义标签类,里面的方法必须是static静态的方法:



  2. 1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    package  com.hanchao.el;
     
    import  java.text.SimpleDateFormat;
    import  java.util.Date;
     
    import  com.hanchao.entity.User;
     
    /**
      * 自定义EL表达式
      * 注意事项:方法必须是static的静态方法
      * @author liweihan (liweihan@sohu-inc.com)
      * @version 1.0 (2014年11月14日 下午2:20:57)
      */
    public  class  MyElTag {
     
         /**
          * 用来验证用户名是否为admin 
          * [仅仅是测试,无意义]
          * @param user   实体类User
          * @return
         
          * 2014年11月14日 下午2:27:14
          * liweihan
          */
         public  static  boolean  checkUsername(User user) {
             if  (user.getName().equals( "admin" )) {
                 return  true ;
             }
             return  false ;
         }
         
         /**
          * 字符串反转
          * @param str 需要反转的字符串
          * @return
         
          * 2014年11月14日 下午2:30:00
          * liweihan
          */
         public  static  String reverse(String str) {
             return  new  StringBuffer(str).reverse().toString();
         }
         
         /**
          * 返回字符串去掉前后空格的字符长度
          * @param str
          * @return
         
          * 2014年11月14日 下午2:31:17
          * liweihan
          */
         public  static  int  countStr(String str) {
             return  str.trim().length();
         }
         
         /**
          * 格式化日期
          * @param date       日期
          * @param pattern    格式
          * @return
         
          * 2014年11月14日 下午3:33:33
          * liweihan
          */
         public  static  String formatTime(Date date ,String pattern) {
             SimpleDateFormat simpleDateFormat =  new  SimpleDateFormat(pattern);
             return  simpleDateFormat.format(date);
         }
    }
  3. 1
    在WEB-INF下面建立一个tld文件:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
<? 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 http://java.sun.com/xml/ns/j2ee/web-jsptaglibrary_2_0.xsd"
   version = "2.0" >
     <!-- 定义函数的版本 -->
     < tlib-version >1.0</ tlib-version >
     < short-name >el</ short-name >
     <!-- 定义函数的名称  →
     <short-name>myel</short-name>
      -->
     < uri >http://so.tv.sohu.com/custom/functions</ uri
       
       <!-- 定义顶一个函数 -->
       < function >
           <!-- 函数描述 -->
           < description >check isOrNot admin</ description >
           <!-- 函数名  → 注意:此处的名字和JSP页面上名字一样!
           <name>checkUsername</name>
           -->
           < name >check</ name >
           <!-- 定义函数处理类 -->
           < function-class >com.hanchao.el.MyElTag</ function-class >
           <!-- 函数参数说明 -->
           < function-signature >
               boolean checkUsername(com.hanchao.entity.User)
           </ function-signature >
           <!-- 例子 -->
           < example >${el:check(user)}</ example >
       </ function >
       
       <!-- 反转一个字符串 -->
       < function >
           < description >reverse a String</ description >
           < name >reverse</ name >
           < function-class >com.hanchao.el.MyElTag</ function-class >
           < function-signature >
               java.lang.String reverse(java.lang.String)
           </ function-signature >
       </ function >
       
       <!-- 去掉前后空格后返回一个字符串的长度 -->
       < function >
           < description >get a String'length</ description >
           < name >len</ name >
           < function-class >com.hanchao.el.MyElTag</ function-class >
           < function-signature >
               java.lang.Integer countStr(java.lang.String)
           </ function-signature >
       </ function >
       
       <!-- 格式化日期 -->
       < function >
           < description >formate date or time</ description >
           < name >format</ name >
           < function-class >com.hanchao.el.MyElTag</ function-class >
           < function-signature >
               java.lang.String formatTime(java.util.Date,java.lang.String)
           </ function-signature >
       </ function >
   
</ taglib >

4.在web.xml中加入jsp-fig的配置

1
2
3
4
5
6
7
8
9
10
   <!-- 自定义EL表达式 -->
   < jsp-config >
       < taglib >
           <!-- 定义标签的引用地址,JSP页面时会用到  ,
           和tld文件的地址保持一致!但是tld文件中可以省略不写-->
           < taglib-uri >/myeltag</ taglib-uri >
           <!-- 配置标签的TLD文件地址 -->
           < taglib-location >/WEB-INF/myel.tld</ taglib-location >
       </ taglib >
   </ jsp-config >


5.JSP页面中使用:注意事项如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ page import="com.hanchao.entity.User" %>
<%@ page import="java.util.Date" %>
<%@ taglib uri="/myeltag" prefix="m"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
< html >
< head >
< meta  http-equiv = "Content-Type"  content = "text/html; charset=UTF-8" >
< title >自定义EL表达式的简单学习</ title >
</ head >
< body >
     < h1 >EL表达式的简单学习</ h1 >
 
     <%
         User user = new User();
         user.setName("admin1");
         request.setAttribute("user", user);
         pageContext.setAttribute("name"," 123456");
         application.setAttribute("date", new Date());
     %>
 
     <%--
         注意事项:
         1.checkUsername的值来源于tld文件的fucntion标签下的name的值!!
         2.myel的值与tld文件的short-name标签里面的值貌似关系不大!
           我们只需要在引入时定义prefix="xx",使用时${xx:}
         
        ${myel:checkUsername(user) }
      --%>
      
      ${m:check(user) }
      
      < hr  />
      ${m:reverse(name) }
      
      < hr  />
      ${m:len(name) }
      
      < hr  />
      ${m:format(date,"yyyy-MM-dd") }
      
      <%--
        参考文章:
        http://954151190.iteye.com/blog/626727
        http://blog.sina.com.cn/s/blog_780a632b0100wrnq.html
       --%>
</ body >
</ html >