步骤
- 该类及其函数,需要在一个拓展名为.tld的xml文件中进行注册
- tld文件需要建立在WEB-INF下。
- xml需要配置文件头
首先创建一个类
在这个类中书写需要的方法
在WEB-INF下创建myFu.tld
文件是一个空的,需要在其中写入相应的约束和配置。
具体约束需要参照你电脑中tomcat安装目录下的:webapps\examples\WEB-INF\jsp2\ jsp2-example-taglib.tld
打开jsp2-example-taglib.tld。
这里就是约束头:
<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"> </taglib>
注意这里的约束头部一定是当前电脑中的文件里复制。
这时你的tld文件会报错,因为你需要写入该约束条件的必要标签。
<description>A tag library exercising SimpleTag handlers.</description> //可以删除
<tlib-version>1.0</tlib-version> //必要版本
<short-name>SimpleTagLibrary</short-name> //jsp文件名称,也可以不改
<uri>http://tomcat.apache.org/jsp2-example-taglib</uri> //这里完全自定义。
这时我写的:
<description>A tag library exercising SimpleTag handlers.</description> <!-- 这是描述信息-->
<tlib-version>1.0</tlib-version>
<short-name>myFu</short-name>
<uri>http://www.baidu.com/jsp/el/funtions</uri>
现在是注册:
该标签还是在jsp2-example-taglib.tld文件中复制
<function>
<description>Converts the string to all caps</description>
<name>方法名</name>
<function-class>类全名</function-class>
<function-signature>java.lang.String 方法名( java.lang.String )</function-signature>
</function>
这是我写的:
<function>
<name>lowerToUpper</name>
<function-class>ly.net.web.function.ELfuntions</function-class>
<function-signature>java.lang.String lowerToUpper( java.lang.String )</function-signature>
</function>
到这里注册就完成了。这里需要注意的是,有多少个方法就要注册多少。
下面就要在jsp中运行了。
这里一定要引入刚才写的uri。
<%@ taglib uri ="http://www.baidu.com/jsp/el/funtions" prefix="myFt" %>
这下就成功啦: