主题:[原创]编写自定义EL函数的步骤
编写自定义EL函数的步骤:
1.编写一个有public的静态方法的java类
2.编写一个标记库描述文件
3.在jsp中使用一个taglib指令
4.使用EL调用函数
我的QQ号:2535279 ,网址:www.javaedu.com.cn
详细请看下面这个例子:
ELFunction.java
public class ELFunction{
public static String sayHello(){
return "Hello,world";
}
public static int max(int x,int y){
return (i>j)?i:j;
}
public static String concat(String str1,Stringstr2){
return str1+str2;
}
}
WEB-INF/*.tld
<?xml version="1.0" encoding="gb2312"?>
<!DOCTYPE taglib
PUBLIC "-Sun MicroSystems,INC.//DTD JSP Tag Library 1.2//EN"
"http:\\java.sun.com\js2ee\dtd\web-jsptaglibrary-1-2.dtd"
>
<taglib>
<tlib-version>1.0</tlib-version>
<jsp-version>2.0</jsp-version>
<short-name>elfn</short-name>
<uri>myelfunction</url>
<function>
<name>sayHello</name>
<function-class>*****</function-class>
<function-signature>String sayHello()</function-signature>
</function>
<function>
<name>max</name>
<function-class>*****</function-class>
<function-signature>int max(int,int)</function-signature>
</function>
<function>
<name>concat</name>
<function-class>*****</function-class>
<function-signature>String concat(java.lang.String,java.lang.String)</function-signature>
</function>
</taglib>
*.jsp
<%@ taglib uri="myelfunction" prefix ="my"%>
${my:sayHello()}
${my:max(20,30)}
${my:concat("Hello","Spring")}
1.编写一个有public的静态方法的java类
2.编写一个标记库描述文件
3.在jsp中使用一个taglib指令
4.使用EL调用函数
我的QQ号:2535279 ,网址:www.javaedu.com.cn
详细请看下面这个例子:
ELFunction.java
public class ELFunction{
public static String sayHello(){
return "Hello,world";
}
public static int max(int x,int y){
return (i>j)?i:j;
}
public static String concat(String str1,Stringstr2){
return str1+str2;
}
}
WEB-INF/*.tld
<?xml version="1.0" encoding="gb2312"?>
<!DOCTYPE taglib
PUBLIC "-Sun MicroSystems,INC.//DTD JSP Tag Library 1.2//EN"
"http:\\java.sun.com\js2ee\dtd\web-jsptaglibrary-1-2.dtd"
>
<taglib>
<tlib-version>1.0</tlib-version>
<jsp-version>2.0</jsp-version>
<short-name>elfn</short-name>
<uri>myelfunction</url>
<function>
<name>sayHello</name>
<function-class>*****</function-class>
<function-signature>String sayHello()</function-signature>
</function>
<function>
<name>max</name>
<function-class>*****</function-class>
<function-signature>int max(int,int)</function-signature>
</function>
<function>
<name>concat</name>
<function-class>*****</function-class>
<function-signature>String concat(java.lang.String,java.lang.String)</function-signature>
</function>
</taglib>
*.jsp
<%@ taglib uri="myelfunction" prefix ="my"%>
${my:sayHello()}
${my:max(20,30)}
${my:concat("Hello","Spring")}