JSP页面中的EL自定义标签及使用

xiaoxiao2021-02-28  117

好记性不如赖笔头…………

创建自定义标签注意事项如下: *1. 创建的类方法必须为静态的。 2. 创建的tld文件,保存位置在WEB-INF文件夹下,不能是lib或classes下 3. tld文件在配置时,除了8种基本类型可以不用写包名外,其它的类型全部要写全限定名。*

1、创建一个类,并定义一个静态方法(注意:非静态方法无法使用),具体代码如下

package com.ckinghan.custom.function; import java.io.Serializable; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Date; public class CustomFunction implements Serializable { /** * 将数字转换为时间 * @param num * @return */ public static String longConvertToDate(long num){ Date date = new Date(num); return date.toLocaleString(); } /** * 将时间转换为数字 * @param date * @return */ public static long dateConvertToLong(String string){ SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd"); Date date; try { date = simpleDateFormat.parse(string); return date.getTime(); } catch (ParseException e) { e.printStackTrace(); return -1; } } }

2、创建tld文件,文件的只有存储在WEB-INF文件夹下,且不能存储在classes中,否则将无法生效,本文件是直接存储在WEB-INF下面的。具体代码如下:

<?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.0.1</tlib-version> <!-- 短名称:在JSP引入页面prefix的值 --> <short-name>cfl</short-name> <!-- uri:绑定在唯一的URI上,URI路径并不一定有数据 --> <uri>http://www.ckinghan.com/customFunction/customFunction</uri> <!-- 定义的方法==将数字转换为时间 --> <function> <!-- 定义的方法名称,可以不与实际的执行方法名相同 --> <name>convertDate</name> <!-- 此方法在项目中的全限定名 --> <function-class>com.ckinghan.custom.function.CustomFunction</function-class> <!-- 要执行的方法,注意:除了基本类型的值外,其它的类型必须写完成的包名.类名(全限定名) --> <function-signature>java.lang.String longConvertToDate(long)</function-signature> </function> <!-- 定义的方法==将时间转换为数字 --> <function> <name>convertLong</name> <function-class>com.ckinghan.custom.function.CustomFunction</function-class> <function-signature>long dateConvertToLong(java.lang.String)</function-signature> </function> </taglib>

3、在web.xml中配置,这一步是可以省掉的,但如果报相关的错误,可以将下面的代码写入到web.xml中,具体代码如下:

<jsp-config> <taglib> <taglib-uri>http://www.ckinghan.com/function/customConvert</taglib-uri> <taglib-location>/WEB-INF/customConvert.tld</taglib-location> </taglib> <taglib> <taglib-uri>http://www.ckinghan.com/customFunction/customFunction</taglib-uri> <taglib-location>/WEB-INF/customFunction.tld</taglib-location> </taglib> </jsp-config>

4、创建JSP文件,引入并使用自定义标签

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <%@ taglib uri="http://www.ckinghan.com/customFunction/customFunction" prefix="cfl"%> <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <base href="<%=basePath%>"> <title>expression</title> <meta http-equiv="pragma" content="no-cache"> <meta http-equiv="cache-control" content="no-cache"> <meta http-equiv="expires" content="0"> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> <meta http-equiv="description" content="This is my page"> <!-- <link rel="stylesheet" type="text/css" href="styles.css"> --> </head> <body> <%-- 将数字转换为时间 --%> <h1>10000000转换成时间为:${cfl:convertDate(10000000) }<br/> <%-- 将时间转换为数字 --%> 1990-01-20转换成数字为:${cfl:convertLong("1990-01-20") }</h1> </body> </html>

效果图如下:

转载请注明原文地址: https://www.6miu.com/read-80633.html

最新回复(0)