jstl自定义标签

阅读更多

自定义标签的步骤:

  1. 定义类和方法(方法必须是public static
  2. 编写自定义 tld文件,放在web-inf目录下
  3. 在jsp中采用taglib指令引用自定义函数库
  4. 调用${前缀:函数名}

 

一下是具体的使用

 

我定义的是一个读取Clob数据类型的类

public class readClob {
 
 public static String read(Clob clob) throws Exception{
  String str = null;
  InputStream stream = clob.getAsciiStream();
  int len = (int)clob.length();
  byte[] b = new byte[len];
  int i;
  while(-1 != (i = stream.read(b, 0, b.length))) {
   stream.read(b, 0, i);
  }
  str = new String();
  return str;
 }
}

 

 

tld文件

  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">
   
  JSTL 1.1 functions library
  myFunction
  1.1
  my//(prefix="my")
  http://www.hdax.com/functions//(url="")
  
    
 
    read//方法名
    utils.readClob//所在的包和类
    java.util.String read(java.sql.Clob)
 

 

 

jsp页面里<%@ taglib uri="http://www.hdax.com/functions" prefix="my" %>

 

实际调用

      
           ${my:read(article.content) }
       

 

你可能感兴趣的:(jstl自定义标签)