zoukankan      html  css  js  c++  java
  • EL表达式(三)自定义 EL 表达式

    自定义EL函数(静态方法):
    编写步骤:
    1.编写一个Java类,提供一个静态方法
    import java.util.List;
    public class GetLength {
    public static Integer getLength(List list){

    return list.size();
    }
    }

    2.在WEB-INF目录下建立一个拓展名为tld(描述文件)的xml文件
    <?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">

    <description>this method can get a list length</description>
    <display-name>getlength</display-name>
    <tlib-version>1.1</tlib-version>
    <short-name>myfn</short-name> <!-- 标准访问前缀 -->
    <uri>http://www.sdxbxx.con/getLength</uri>
    <function> <!-- 定义函数 -->
    <description>this method can get a list length</description>
    <name>getLength</name>
    <function-class>cn.gs.wwg.el.GetLength</function-class>
    <function-signature>java.lang.Integer getLength(java.util.List)</function-signature>
    </function>
    </taglib>
    3.(可选)告知应用tld文件和tld中的uri对应
    <jsp-config>
    <taglib>
    <taglib-uri>http://www.sdxbxx.con/getLength</taglib-uri>
    <taglib-location>/WEB-INF/getLength.tld</taglib-location>
    </taglib>
    </jsp-config>

    2.在WEB-INF目录下建立一个拓展名为tld(标签描述文件)的xml文件
    3.(可选)告知应用tld文件和tld中的uri对应

    1. Java

    1 package cn.gs.ly.mvc.domain;
    2 import java.util.List;
    3 public class GetLength {
    4     public static Integer getLength(List list){
    5         
    6         return list.size();
    7     }
    8 }

    2. jsp

     1 <%@page import="java.util.HashMap"%>
     2 <%@page import="java.util.Map"%>
     3 <%@page import="java.util.ArrayList"%>
     4 <%@page import="java.util.List"%>
     5 <%@page import="cn.gs.ly.mvc.domain.Person"%>
     6 <%@ taglib uri="/WEB-INF/getLength.tld" prefix="myfn" %>
     7 <%@ taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn" %>
     8 <%@ taglib uri="http://www.liuyang.com/getLength" prefix="myfn1" %>
     9 <%@ page language="java" contentType="text/html" pageEncoding="UTF-8"%>
    10 <!DOCTYPE html>
    11 <html>
    12 <head>
    13 <title>Insert title here</title>
    14 </head>
    15 <body>
    16     <%
    17         List list = new ArrayList();
    18         list.add("a");
    19         list.add("b");
    20         list.add("c");        
    21         pageContext.setAttribute("list", list);
    22         
    23     %>
    24     ${myfn:getLength(list)}<hr/>
    25     ${fn:contains("abcd","ab") }<hr/>
    26     ${fn:contains("abcd","abd") }<hr/>
    27     
    28     ${fn:split("2018-1-4--14:41:140","-:")[0] }<hr/>
    29     ${fn:split("2018-1-4--14:41:140","-:")[1] }<hr/>
    30     ${fn:split("2018-1-4--14:41:140","-:")[2] }<hr/>
    31     ${fn:split("2018-1-4--14:41:140","-:")[3] }<hr/>
    32     ${fn:split("2018-1-4--14:41:140","-:")[4] }<hr/>
    33     ${fn:split("2018-1-4--14:41:140","-:")[5] }<hr/>
    34     ${fn:split("2018-1-4--14:41:140","-:")[6] }<hr/>
    35     
    36     
    37     
    38 </body>
    39 </html>

    3. 在WEB-INF目录下建立一个拓展名为tld(描述文件)的xml文件

     1 <?xml version="1.0" encoding="UTF-8" ?>
     2 <taglib 
     3     xmlns="http://java.sun.com/xml/ns/j2ee"
     4       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     5       xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-jsptaglibrary_2_0.xsd"
     6       version="2.0">
     7     
     8       <description>this method can get a List length</description>
     9       <display-name>getLength</display-name>
    10       <tlib-version>1.1</tlib-version>
    11       <short-name>myfn</short-name>
    12       <uri>http://www.liuyang.com/getLength</uri>
    13       <function>
    14         <description>this method can get a List length </description>
    15         <name>getLength</name>
    16         <function-class>cn.gs.ly.mvc.domain.GetLength</function-class>
    17         <function-signature>java.long.Integer getLength(java.util.List)</function-signature>
    18       </function> 
    19 </taglib>

    4. 配置web.xml 文件

     1 <?xml version="1.0" encoding="UTF-8"?>
     2 <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
     3   <display-name>WEB2018</display-name>
     4   <welcome-file-list>
     5     <welcome-file>index.html</welcome-file>
     6     <welcome-file>index.htm</welcome-file>
     7     <welcome-file>index.jsp</welcome-file>
     8     <welcome-file>default.html</welcome-file>
     9     <welcome-file>default.htm</welcome-file>
    10     <welcome-file>default.jsp</welcome-file>
    11   </welcome-file-list>    
    12       
    13       <context-param><!-- 初始化参数 -->
    14           <param-name>info</param-name>
    15           <param-value>value_liuyang</param-value>
    16       </context-param>     
    17       
    18       <jsp-config>
    19           <taglib>
    20               <taglib-uri>http://www.liuyang.com/getLength</taglib-uri>
    21               <taglib-location>/WEB-INF/getLength.tld</taglib-location>
    22           </taglib>
    23       </jsp-config>
    24       
    25   
    26 </web-app>
  • 相关阅读:
    isMemberOfClass和isKindOfClass的区别
    performSelector和respondsToSelector用法
    iOS:堆(heap)和栈(stack)的理解
    iOS 事件处理机制与图像渲染过程
    IOS总结 静变量static、全局变量extern、局部变量、实例变量
    创建mvc
    程序启动原理
    常用几个UITableView,UICollectionView  UIScrollView关键点
    代码初始化 故事板初始化 xib初始化总结
    UINavigationController  和 UITabBarController
  • 原文地址:https://www.cnblogs.com/liuyangv/p/8195222.html
Copyright © 2011-2022 走看看