TA的每日心情 | 开心 2021-3-12 23:18 |
---|
签到天数: 2 天 [LV.1]初来乍到
|
1)编写函数(Functions.java)
package jsp2.examples.el;
import java.util.*;
/**
* Defines the functions for the jsp2 example tag library.
*
* <p>Each function is defined as a static method.</p>
*/
public class Functions {
public static String reverse( String text ) {
return new StringBuffer( text ).reverse().toString();
}
public static int numVowels( String text ) {
String vowels = "aeiouAEIOU";
int result = 0;
for( int i = 0; i < text.length(); i++ ) {
If( vowels.indexOf( text.charAt( i ) ) != -1 ) {
result++;
}
}
return result;
}
public static String caps( String text ) {
return text.toUpperCase();
}
}
所有的函数都必须是public static
2)编写tld文件example-taglib.tld
<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>A tag library exercising SimpleTag handlers.</description>
<tlib-version>1.0</tlib-version>
<short-name>SimpleTagLibrary</short-name>
<uri>/SimpleTagLibrary</uri>
<function>
<description>Reverses the characters in the given String</description>
<name>reverse</name>
<function-class>jsp2.examples.el.Functions</function-class>
<function-signature>java.lang.String reverse( java.lang.String )</function-signature>
</function>
<function>
<description>Counts the number of vowels (a,e,i,o,u) in the given String</description>
<name>countVowels</name>
<function-class>jsp2.examples.el.Functions</function-class>
<function-signature>java.lang.String numVowels( java.lang.String )</function-signature>
</function>
<function>
<description>Converts the string to all caps</description>
<name>caps</name>
<function-class>jsp2.examples.el.Functions</function-class>
<function-signature>java.lang.String caps( java.lang.String )</function-signature>
</function>
</taglib>
3)编写web.xml文件
-
<
jsp-config
>
-
<
taglib
>
<
taglib-uri
>
http://jakarta.apache.org/tomcat/examples-taglib
</
taglib-uri
>
<
taglib-location
>
/WEB-INF/jsp/example-taglib.tld
</
taglib-location
>
</
taglib
>
-
</
jsp-config
>
4)使用自定义EL函数
<%@ taglib prefix="my" uri="http://jakarta.apache.org/tomcat/jsp2-example-taglib"%>
<HTML>
<head>
<title>JSP 2.0 Expression Language - Functions</title>
</head>
<body>
<h1>JSP 2.0 Expression Language - Functions</h1>
<hr>
An upgrade from the JSTL expression language, the JSP 2.0 EL also
allows for simple function invocation. Functions are defined
by tag libraries and are implemented by a Java programmer as
static methods.
<blockquote>
<u><b>Change Parameter</b></u>
<form action="functions.jsp" method="GET">
foo = <input type="text" name="foo" >
<input type="submit">
</form>
<br>
<code>
<table border="1">
<thead>
<td><b>EL Expression</b></td>
<td><b>Result</b></td>
</thead>
<tr>
<td>$ {param["foo"]}</td>
<td>$ {fn:escapeXml(param["foo"])} </td>
</tr>
<tr>
<td>$ {my:reverse(param["foo"])}</td>
<td>$ {my:reverse(fn:escapeXml(param["foo"]))} </td>
</tr>
<tr>
<td>$ {my:reverse(my:reverse(param["foo"]))}</td>
<td>$ {my:reverse(my:reverse(fn:escapeXml(param["foo"])))} </td>
</tr>
<tr>
<td>$ {my:countVowels(param["foo"])}</td>
<td>$ {my:countVowels(fn:escapeXml(param["foo"]))} </td>
</tr>
</table>
</code>
</blockquote>
</body>
</html>
源码下载:http://file.javaxxz.com/2014/10/28/235608265.zip |
|