您的位置:首页 > Web前端 > JavaScript

JSP学习笔记(七):自定义方法标签

2015-02-17 09:58 507 查看
JSTL提供了一些基本的方法,封装在 funtion下,如${fn:length('')}等。便签的方法也是可以自由扩展的。

一、自定义方法
1、标签的自定义方法实际上是一些类的静态方法。不需要实现任何借口继承任何类。

(一)简单实例
<span style="font-family:Arial;">1、代码:
package function;
import java.util.Collection;
/**
*
* @Title: 自定义的Function
* @Description:
* @Copyright: Copyright (c) 2015
* @Company:
*
* @author: SAM-SHO
* @version: 1.0
* @CreateDate:Feb 16, 2015
*/
public class Function {
/**
* 返回字节长度
*
* @param obj
* @return
*/
public static int length(Object obj) {
if (obj == null)
return 0;
if (obj instanceof StringBuffer) {
return length(((StringBuffer) obj).toString());
}
if (obj instanceof String) {
return ((String) obj).getBytes().length;
}
if (obj instanceof Collection) {
return ((Collection) obj).size();
}
return 0;
}
public static String substring(String str, int byteLength) {
if (str == null)
return "";
StringBuffer buffer = new StringBuffer();
for (int i = 0; i < str.length(); i++) {
char ch = str.charAt(i);
if (length(buffer.toString() + ch) > byteLength) {
break;
} else {
buffer.append(ch);
}
}
return buffer.toString();
}
public static void main(String[] args) {
System.out.println(length("中文测试"));
System.out.println(substring("中文测试", 5));
}
}
// end</span>


2、tld文件:function.tld,默认需要在WEB-INF/下
<span style="font-family:Arial;"><?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>custom functions library</description>
<display-name>custom functions</display-name>
<tlib-version>1.1</tlib-version>
<short-name>function</short-name>
<uri>http://www.myFunction.com/function</uri>

<function>
<description>
</description>
<name>length</name>
<function-class>function.Function</function-class>
<function-signature>int length(java.lang.Object)</function-signature>
<example>
${fn:length(string)}
</example>
</function>

<function>
<description>
</description>
<name>substring</name>
<function-class>function.Function</function-class>
<function-signature>java.lang.String substring(java.lang.String, int)</function-signature>
<example>
${fn:length(string, 3)}
</example>
</function>

</taglib>
</span>


1)自定义方法的声明写在 <function-signature>标记里,
格式为”返回值 方法名(参数一类型 参数二类型)“,如int length(java.lang.Object)
2)可以在web.xml中指定位置:
<span style="font-family:Arial;">  	<taglib>
<taglib-uri>http://www.myFunction.com/function</taglib-uri>
<taglib-location>/WEB-INF/function.tld</taglib-location><!--定义tld文件位置   -->
</taglib>
</span>


3)调用:
<span style="font-family:Arial;"><%@ page language="java" contentType="text/html; charset=UTF-8"%>
<%@ taglib uri="http://www.myFunction.com/function" prefix="fn"%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
<style type="text/css">
table {
border-collapse: collapse;
border: 1px solid #000000;
}
td {
font-size: 12px;
border: 1px solid #000000;
padding: 2px;
}
.l{
font-weight: bold;
}
</style>
</head>
<body>

<%
request.setAttribute("string", "字符串测试");
%>

<table>
<tr>
<td class="l">字符串变量</td>
<td>${ string }</td>
</tr>
<tr>
<td class="l">字符串长度(按字节计)</td>
<td>${ fn:length(string) }</td>
</tr>
<tr>
<td class="l">截取 7 个字节</td>
<td>${ fn:substring(string, 7) }</td>
</tr>
</table>

</body>
</html></span>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: