1.什么是EL自定义函数
EL自定义函数是在EL表达式中调用的某个java类的静态方法,这个静态方法需在web应用程序中进行配置才可以被EL表达式调用。
EL自定义函数可以扩展EL表达式的功能,让EL表达式完成普通java程序代码所能完成的功能。
2.EL自定义函数开发步骤
编写EL自定义函数映射的java类中的静态方法:这个Java类必须带有public修饰符,方法必须是这个类的带有public修饰符的静态方法;
编写标签库描述文件(tld文件),在tld文件中描述自定义函数;
在jsp页面中导入和使用自定义函数。
3.示例代码
实现的功能是连接两个字符串。
编写静态方法,有public修饰符,且为静态方法,elFunction.java
package com.javaweb.tag;
public class elFunction {
public static String concat(String str1,String str2){
return str1+str2;
}
}
编写标签库描述文件,即tld文件,相关的自定义函数的描述在function标签中,elFunction.tld
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-jsptaglibrary_2_1.xsd"
version="2.1">
MyTag 1.1 core library
MyTag core
1.1
c
http://java.www.com/jsp/jstl/core/elFunction
concat
com.javaweb.tag.elFunction
java.lang.String concat(java.lang.String,java.lang.String)
jsp文件中导入和使用自定义函数。
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
My JSP 'elFunction.jsp' starting page${koala:concat(param.name1,param.name2)}
运行后输出结果为:
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持我们。