自定義標籤

[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>
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章