自定义JSTL函数标签(一)

jstl标签库的配置
* 将jstl.jar和standard.jar拷贝到WEB-INF/lib下(如果使用el表达式,不用拷贝这两个jar)

注意:jstl必须在能够支持j2ee1.4/servlet2.4/jsp2.0版本上的容器才能运行,这个环境
      是目前较为常用的环境

     
标签库的使用
* 采用taglib指令引入
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt"%>

  
自定义函数库:
1、定义类和方法(方法必须是public static
2、编写自定义tld文件,并且将此文件放到WEB-INFWEB-INF任意子目录下
3、在jsp中采用taglib指令引入自定义函数库
4、采用 前缀+冒号(:)+函数名 调用即可


以下通过显示省份来看实现步骤:


第一步:新建一个类如下:
UtilFunction.java

Java代码
    package demo;   
      
    import java.util.ArrayList;   
    import java.util.List;   
      
    // 测试   
    // 自定义JSTL函数   
    public class UtilFunction {   
        // 获取省份   
        public static List getProvinces() {   
             List provinces = new ArrayList();   
               
            // 暂时添加几个测试   
             provinces.add("广东省");   
             provinces.add("广西省");   
             provinces.add("山东省");   
             provinces.add("四川省");   
             provinces.add("江西省");   
               
            return provinces;      
         }      
    }  

第二步:编写tld标签函数注册文件
myfunctions.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">  
           
      <tlib-version>1.0</tlib-version>  
      <short-name>my</short-name>  
      <uri>http://www.changtusoft.cn/test/functions</uri>   (其中,若uri为/WEB-INF/xxx.tld,则无需再下面tomcat中注册)
         
      <!-- JSTL自定义函数   获取省份 -->  
      <function>  
        <name>getProvinces</name>  
        <function-class>demo.UtilFunction</function-class>  
        <function-signature>java.util.List getProvinces()</function-signature>  
      </function>  
    </taglib>  

第三步:在web.xml文件中注册tld

Xml代码
    <?xml version="1.0" encoding="UTF-8"?>  
    <web-app version="2.5"   
        xmlns="http://java.sun.com/xml/ns/javaee"   
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"   
        xsi:schemaLocation="http://java.sun.com/xml/ns/javaee   
         http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">  
      <welcome-file-list>  
        <welcome-file>index.jsp</welcome-file>  
      </welcome-file-list>  
         
      <!-- 注册JSTL函数 -->  
      <jsp-config>  
        <taglib>  
            <taglib-uri>http://www.changtusoft.cn/test/functions</taglib-uri>  
            <taglib-location>/WEB-INF/myfunctions.tld</taglib-location>  
        </taglib>  
      </jsp-config>  
         
    </web-app>  

第四步:编写jsp进行测试
index.jsp

Java代码
    <%@ page language="java" import="java.util.*" pageEncoding="gbk"%>   
    <!-- 导入jstl标签库 -->   
    <%@ taglib prefix="c"   uri="http://java.sun.com/jsp/jstl/core" %>   
    <!-- 导入自定义jstl函数 -->   
    <%@ taglib prefix="my" uri="http://www.changtusoft.cn/test/functions" %>   
      
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">   
    <html>   
       <head>   
         <title>自定义JSTL函数</title>   
       </head>   
         
       <body>   
         省份:   
         <select name="provinces">   
         <option>--请选择省份--</option>   
         <c:forEach items="${my:getProvinces()}" var="p">   
             <option>${p }</option>   
         </c:forEach>   
         </select>   
       </body>   
    </html>  



部署例子到tomcat测试: http://localhost:8080/jstl_functions/index.jsp

结果可以显示省份下拉框表示成功...

注意:

  

可能出现的异常

1、The function xxx must be used with a prefix when a default namespace is not specified

--- 在jsp页面中调用方式不正确,可能将 ":" 写成了 "."

2、The function xxx cannot be located with the specified prefix

--- a) 类中定义的方法不是 public static 的方法

      b) 类中的方法名称和jsp自带的标签元素冲突,重名等

原文地址:https://www.cnblogs.com/chen-lhx/p/5373816.html