---
title: "自定义标签"
author: "kidd"
date: "2009-02-16T16:23:20Z"
tags: []
slug: "untitled"
draft: false
lastmod: "2009-02-20T17:02:56Z"
id: 333672
url: "https://kidd.iteye.com/blog/333672"
---

<span style="color:blue">web.xml代码：下 </span>

<jsp-config> 
<taglib> 
<taglib-uri>/my-function</taglib-uri> 
<taglib-location> 
/WEB-INF/tags/my-function.tld 
</taglib-location> 
</taglib> 
</jsp-config> 

<span style="color:blue">function.tld代码：下</span>

<?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>


<span style="color:blue">MyFunction.java代码：下
(可在MyFunction类中根据需要写如自己的方法)</span>

public class MyFunction {
	public static String getSex(String sex) {
		return ("M".equals(sex) ? "男" : "女");
	}
}


<span style="color:blue">test.jsp代码：下</span>

<%@ page language="java" contentType="text/html; charset=GBK"
	pageEncoding="GBK"%>

<span style="color:red"><%@ taglib prefix="my" uri="/my-function"%></span>
<html>
	<head>
		<title>个人信息</title>
	</head>

	<body>
		<tableborder="1" align="center">
			<tr>
				<th>
					性&nbsp;&nbsp;&nbsp;&nbsp;别
				</th>
				<td>
					<span style="color:red">${my:getSex(sex)}</span>
				</td>
			</tr>

		</table>
	</body>
</html>