自定义标签

本文介绍如何通过自定义JSP标签库实现一个简单的性别转换功能,包括配置web.xml,定义tld文件,实现Java类以及在JSP页面中的使用方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

[color=blue]web.xml代码:下 [/color]

<jsp-config>
<taglib>
<taglib-uri>/my-function</taglib-uri>
<taglib-location>
/WEB-INF/tags/my-function.tld
</taglib-location>
</taglib>
</jsp-config>

[color=blue]function.tld代码:下[/color]

<?xml version="1.0" encoding="UTF-8"?>
<taglib version="2.0" 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">
<description>处理EL转换的自定义函数</description>
<tlib-version>1.0</tlib-version>
<short-name>my-function</short-name>
<uri>/my-function</uri>

<function>
<description>获取性别(男/女)</description>
<name>getSex</name>
<function-class>com.util.MyFunction</function-class>
<function-signature>
java.lang.String getSex( java.lang.String )
</function-signature>
</function>
</taglib>


[color=blue]MyFunction.java代码:下
(可在MyFunction类中根据需要写如自己的方法)[/color]

public class MyFunction {
public static String getSex(String sex) {
return ("M".equals(sex) ? "男" : "女");
}
}


[color=blue]test.jsp代码:下[/color]

<%@ page language="java" contentType="text/html; charset=GBK"
pageEncoding="GBK"%>

[color=red]<%@ taglib prefix="my" uri="/my-function"%>[/color]
<html>
<head>
<title>个人信息</title>
</head>

<body>
<tableborder="1" align="center">
<tr>
<th>
性    别
</th>
<td>
[color=red]${my:getSex(sex)}[/color]
</td>
</tr>

</table>
</body>
</html>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值