编写描述的tld文件放到web-inf/目录下,才能在jsp页面上调用
<?xml version="1.0" encoding="UTF-8" ?> <taglib xmlns="http://java.sun.com/xml/ns/javaee" 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"> <tlib-version>1.0</tlib-version> <short-name>lf</short-name> <uri>www.com.zr.lf.fun</uri> <!-- 指定访问的路径 --> <function> <name>strLen</name> <!-- 指定EL自定义函数的名称 --> <function-class>com.zr.testelfun.function.TestFunction</function-class><!-- 指定完整的Java类名 --> <!-- 指定Java类中的静态方法签名,必须指明方法的返回值类型及各个参数的类型,各个参数之间用逗号分隔 --> <function-signature> int getStrLength(java.lang.String) </function-signature> </function> </taglib>
package com.zr.testelfun.function; public class TestFunction { public static int getStrLength(String str){ return str.length(); } }
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <%@ taglib prefix="lf" uri="www.com.zr.lf.fun" %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <title>test</title> </head> <body> <h1>字符串长度:${lf:strLen("adcc") } </h1> </body> </html>
时间: 2024-10-07 06:47:31