JSTL标签急速秒杀jsp页面中的java代码---自定义函数

JSTL函数标签库为我们提供了16常用函数,这些函数的使用大大简化了jsp页面代码,同时提高了程序的可维护性,但是你有没有想过既然称之为标签“库”,那么它肯定是可扩展的。通过对函数标签库的扩展我们可以轻松实现自定义的函数标签功能,使程序开发变得更加简便。下面以通过实例手把手教你怎样DIY自己的函数标签。

1.定义自己的功能类MyFunctions。注意在该类中将要被使用的方法必须为静态的全局方法

  1. public class MyFunctions {  
  2.   
  3.     /** 
  4.      * 必须为静态全局的 
  5.      * @param username 
  6.      * @return 
  7.      */  
  8.     public static String sayHello(String username){  
  9.         return "Hello " + username;  
  10.     }  
  11. }  
public class MyFunctions {

	/**
	 * 必须为静态全局的
	 * @param username
	 * @return
	 */
	public static String sayHello(String username){
		return "Hello " + username;
	}
}
2.自定义tld描述文件,然后将该tld文件放置到WEB-INF或WEB-INF下的任意目录中。本例中tld文件内容如下:

  1. <?xml version="1.0" encoding="UTF-8" ?>  
  2.   
  3. <taglib 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>my functions library</description>  
  9.   <display-name>my functions</display-name>  
  10.   <tlib-version>1.1</tlib-version>  
  11.   <short-name>my</short-name>  
  12.   <uri>http://mytld.aaa.com</uri>  
  13.     
  14.     
  15.   <function>  
  16.     <name>say</name>  
  17.     <function-class>com.bjpowernode.jstl.MyFunctions</function-class>  
  18.     <function-signature>java.lang.String sayHello(java.lang.String)</function-signature>  
  19.   </function>  
  20. </taglib>  
<?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>my functions library</description>
  <display-name>my functions</display-name>
  <tlib-version>1.1</tlib-version>
  <short-name>my</short-name>
  <uri>http://mytld.aaa.com</uri>
  
  
  <function>
    <name>say</name>
    <function-class>com.bjpowernode.jstl.MyFunctions</function-class>
    <function-signature>java.lang.String sayHello(java.lang.String)</function-signature>
  </function>
</taglib>
tld描述文件中uri标签中的内容原则上是可以随便写的,但是必须要保证两点,一是该uri必须与其他tld描述文件中的uri不相同,二是在引入的时候jsp页面中的taglib指令中uri属性的值必须与该描述文件中的uri相同。另外,在function标签中,name标签为自定义的函数名,该函数名为使用EL时候所调用的函数名function-class标签为真正实现该方法的类的完整路径,function-signature标签为该函数所调用的该类中的真正方法,所需要注意的是在该标签中必须写明该方法的返回值类型,函数名以及参数类型。

3.在jsp中采用taglib指令引入函数库,并在EL表达式中采用前置 + 冒号 + 函数名的方式进行调用。

  1. <%@ page language="java" contentType="text/html; charset=GB18030"  
  2.     pageEncoding="GB18030"%>  
  3. <!--采用taglib指令引入函数库,一定要注意该标签中uri属性值必须与tld文件中的uri相一致 -->  
  4. <%@ taglib uri="http://mytld.aaa.com" prefix="my" %>   
  5. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">  
  6. <html>  
  7. <head>  
  8. <meta http-equiv="Content-Type" content="text/html; charset=GB18030">  
  9. <title>Insert title here</title>  
  10. </head>  
  11. <body>  
  12.        <!--调用自定义函数say() -->  
  13.        自定义函数:${my:say("张三") }<br>  
  14.   
  15.  </body>  
  16. </html>  
<%@ page language="java" contentType="text/html; charset=GB18030"
    pageEncoding="GB18030"%>
<!--采用taglib指令引入函数库,一定要注意该标签中uri属性值必须与tld文件中的uri相一致 -->
<%@ taglib uri="http://mytld.aaa.com" prefix="my" %> 
<!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=GB18030">
<title>Insert title here</title>
</head>
<body>
       <!--调用自定义函数say() -->
       自定义函数:${my:say("张三") }<br>

 </body>
</html>

至此自定义函数的过程讲解完毕。通过JSTL的使用,在jsp页面中出现的将全部是用于显示内容的标签,使得jsp的作用更加专一(只用于显示)。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值