600字范文,内容丰富有趣,生活中的好帮手!
600字范文 > 自定义jstl标签库

自定义jstl标签库

时间:2023-12-03 09:53:45

相关推荐

自定义jstl标签库

开发环境:Spring+SpringMVC +Maven +Mybatis

JSTL 标签库的配置:

导入对应的 jstl.jar 和 standard.jar ,我使用的配置如下:

<properties><standard.version>1.1.2</standard.version><jstl.version>1.2</jstl.version><servlet-api.verison>3.1.0</servlet-api.verison><jsp-api.version>2.2</jsp-api.version></properties></dependencies><dependency><groupId>taglibs</groupId><artifactId>standard</artifactId><version>${standard.version}</version><type>jar</type></dependency><dependency><groupId>javax.servlet</groupId><artifactId>jstl</artifactId><version>${jstl.version}</version><type>jar</type></dependency><dependency><groupId>javax.servlet</groupId><artifactId>javax.servlet-api</artifactId><version>${servlet-api.verison}</version><scope>provided</scope></dependency><dependency><groupId>javax.servlet.jsp</groupId><artifactId>jsp-api</artifactId><version>${jsp-api.version}</version><scope>provided</scope></dependency></dependencies>

注意:jstl必须在能够支持j2ee1.4/servlet2.4/jsp2.0版本上的容器才能运行,这个环境是目前较为常用的环境

标签库的使用:

采用 taglib 指令引入

<%@ taglib prefix="c" uri="/jsp/jstl/core" %>

<%@ taglib prefix="fmt" uri="/jsp/jstl/fmt" %>

自定义函数库:

1、定义类和方法 (方法必须是 public static

2、编写自定义 tld 方法,并且将此文件放到WEB-INFWEB-INF 任意子目录下

3、在jsp 中采用 taglib 指令引入自定义函数库

4、采用前缀+冒号(:)+函数名调用即可

例:

DateUtils

package com.mon.utils;import java.util.Date;import mons.lang3.time.DateFormatUtils;/*** 日期工具类, 继承mons.lang.time.DateUtils类* */public class DateUtils extends mons.lang3.time.DateUtils{private static String[] parsePatterns = {"yyyy-MM-dd", "yyyy-MM-dd HH:mm:ss", "yyyy-MM-dd HH:mm", "yyyy-MM", "yyyy/MM/dd", "yyyy/MM/dd HH:mm:ss", "yyyy/MM/dd HH:mm", "yyyy/MM","yyyy.MM.dd", "yyyy.MM.dd HH:mm:ss", "yyyy.MM.dd HH:mm", "yyyy.MM"};/*** 得到当前日期字符串 格式(yyyy-MM-dd) pattern可以为:"yyyy-MM-dd" "HH:mm:ss" "E"*/public static String getDate(String pattern){return DateFormatUtils.format(new Date(), pattern);}}

fns.tld 自定义标签

<?xml version="1.0" encoding="UTF-8" ?><taglib xmlns="/xml/ns/j2ee"xmlns:xsi="/2001/XMLSchema-instance"xsi:schemaLocation="/xml/ns/j2ee /xml/ns/j2ee/web-jsptaglibrary_2_0.xsd"version="2.0"><description>JSTL 1.1 functions library</description><display-name>JSTL functions sys</display-name><tlib-version>1.1</tlib-version><short-name>fns</short-name><uri>/jsp/jstl/functionss</uri><!-- DateUtils --><function><description>获取当前日期</description><name>getDate</name><function-class>com.mon.utils.DateUtils</function-class><function-signature>java.lang.String getDate(java.lang.String)</function-signature><example>${fns:getDate(pattern)}</example> </function></taglib>

在jsp 中引入该文件

<%@ taglib prefix="fns" uri="/WEB-INF/tlds/fns.tld" %>

prefix 对应

<short-name>fns</short-name>

uri 对应 1) 该tlds 在项目中的路径

2) <uri>/jsp/jstl/functionss</uri>

此路径则需要在 web.xml 中配置引进的路径

<jsp-config> <taglib> <taglib-uri>/jsp/jstl/functionss</taglib-uri> <taglib-location>/WEB-INF/tld/fns.tld</taglib-location> </taglib> </jsp-config>

然后在jsp 中使用该标签

${fns:getDate("yyyy-MM-dd HH:mm")}

注意:

可能出现的异常

1、The function xxx must be used with a prefix when a default namespace is not specified

--- 在jsp页面中调用方式不正确,可能将 ":" 写成了 "."

2、The function xxx cannot be located with the specified prefix

--- a) 类中定义的方法不是 public static 的方法

b) 类中的方法名称和jsp自带的标签元素冲突,重名等

参考链接:/wlxtaking/article/details/5050616

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。