分享web开发知识

注册/登录|最近发布|今日推荐

主页 IT知识网页技术软件开发前端开发代码编程运营维护技术分享教程案例
当前位置:首页 > 教程案例

JSP自定义tld方法标签

发布时间:2023-09-06 01:52责任编辑:彭小芳关键词:暂无标签

 卧槽

我们可以通过tld文件,自定义一个方法标签,以便在页面中使用,目录通常放在WEB-INF下面的tlds文件夹:

引入方式示例,直接在jsp上引入tld标签文件:

<%@ taglib prefix="fns" uri="/WEB-INF/tlds/fns.tld" %>
fns.tld中的写法:
<?xml version="1.0" encoding="UTF-8" ?><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"> ?????<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>http://java.sun.com/jsp/jstl/functionss</uri><!-- DictUtils --> ?<function> ???<description>获取字典标签</description> ???<name>getDictLabel</name> ???<function-class>com.jeeplus.modules.sys.utils.DictUtils</function-class> ???<function-signature>java.lang.String getDictLabel(java.lang.String, java.lang.String, java.lang.String)</function-signature> ???<example>${fns:getDictLabel(value, type, defaultValue)}</example> ???</function> ???<function> ???<description>获取字典标签(多个)</description> ???<name>getDictLabels</name> ???<function-class>com.jeeplus.modules.sys.utils.DictUtils</function-class> ???<function-signature>java.lang.String getDictLabels(java.lang.String, java.lang.String, java.lang.String)</function-signature> ???<example>${fns:getDictLabels(values, type, defaultValue)}</example> ???</function> ?<function> ???<description>获取字典值</description> ???<name>getDictValue</name> ???<function-class>com.jeeplus.modules.sys.utils.DictUtils</function-class> ???<function-signature>java.lang.String getDictValue(java.lang.String, java.lang.String, java.lang.String)</function-signature> ???<example>${fns:getDictValue(label, type, defaultValue)}</example> ???</function>
注:
function-class就是该方法的实体所在类路径,

function-signature就是该方法的方法名,这个方法必须是个static方法。
example就是使用方式写法示例

后台:

public static String getDictLabel(String value, String type, String defaultValue){ ???????if (StringUtils.isNotBlank(type) && StringUtils.isNotBlank(value)){ ???????????for (Dict dict : getDictList(type)){ ???????????????if (type.equals(dict.getType()) && value.equals(dict.getValue())){ ???????????????????return dict.getLabel(); ???????????????} ???????????} ???????} ???????return defaultValue; ???} ???????public static String getDictLabels(String values, String type, String defaultValue){ ???????if (StringUtils.isNotBlank(type) && StringUtils.isNotBlank(values)){ ???????????List<String> valueList = Lists.newArrayList(); ???????????for (String value : StringUtils.split(values, ",")){ ???????????????valueList.add(getDictLabel(value, type, defaultValue)); ???????????} ???????????return StringUtils.join(valueList, ","); ???????} ???????return defaultValue; ???} ???public static String getDictValue(String label, String type, String defaultLabel){ ???????if (StringUtils.isNotBlank(type) && StringUtils.isNotBlank(label)){ ???????????for (Dict dict : getDictList(type)){ ???????????????if (type.equals(dict.getType()) && label.equals(dict.getLabel())){ ???????????????????return dict.getValue(); ???????????????} ???????????} ???????} ???????return defaultLabel; ???}

JSP页面中使用:

  <td> ??????${fns:getDictLabel(preparationManage.preparationMode, ‘samplingMode‘, ‘‘)} ????????????
??</td>

JSP自定义tld方法标签

原文地址:https://www.cnblogs.com/qingo00o/p/8989976.html

知识推荐

我的编程学习网——分享web前端后端开发技术知识。 垃圾信息处理邮箱 tousu563@163.com 网站地图
icp备案号 闽ICP备2023006418号-8 不良信息举报平台 互联网安全管理备案 Copyright 2023 www.wodecom.cn All Rights Reserved