定义和使用EL函数

 

package mypack; 

import java.util.*; 

 

public class Tool { 

    public static int add(String x, String y) { 

        int a = 0; 

        int b = 0; 

        try { 

            a = Integer.parseInt(x); 

            b = Integer.parseInt(y); 

        }catch(Exception e) {}    

    

        return a + b; 

    } 

 

    public static String convert(String s,String encode1,String encode2){ 

        try{ 

            return new String(s.getBytes(encode1),encode2); 

        }catch(Exception e){return null;} 

     } 

} 

 

<%@ page contentType="text/html; charset=GB2312" %> 

<%@ taglib prefix="mm" uri="/mytaglib" %> 

<html> 

<head> 

<title>functions</title> 

</head> 

 

<body> 

 

<h3>Add Numbers</h3> 

<p> 

<form action="sum.jsp" method="get"> 

     user= <input type="text" name="user"    

                value="${mm:convert(param.user,'ISO-8859-1','GB2312')}"> 

     <br> 

    

     x = <input type="text" name="x" value="${param.x}"> 

     <br> 

     y = <input type="text" name="y" value="${param.y}"> 

     <input type="submit" value="Add Numbers"> 

</form> 

 

<p> 

the sum is: ${mm:add(param.x,param.y)} 

 

</body> 

</html>

mytaglib.tld

<?xml version="1.0" encoding="ISO-8859-1" ?> 

 

<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 web-jsptaglibrary_2_0.xsd" 

        version="2.0"> 

        <tlib-version>1.1</tlib-version> 

        <short-name>mytaglib</short-name> 

        <uri>/mytaglib</uri> 

         

 

    <function> 

        <description>add x and y</description> 

        <name>add</name> 

        <function-class>mypack.Tool </function-class> 

        <function-signature> 

             int add(java.lang.String,java.lang.String) 

        </function-signature> 

    </function> 

 

    <function> 

        <description>convert encoding</description> 

        <name>convert</name> 

        <function-class>mypack.Tool </function-class> 

        <function-signature> 

             java.lang.String convert(java.lang.String,java.lang.String,java.lang.String) 

        </function-signature> 

    </function> 

 

</taglib> 

web.xml

    

    <taglib> 

            <taglib-uri>/mytaglib</taglib-uri> 

            <taglib-location>/WEB-INF/mytaglib.tld</taglib-location> 

     </taglib>

 

从 <http://ndebyq.blog.51cto.com/274018/148005> 插入

 

 

 

源文档 <http://hi.baidu.com/cliff77/blog/item/2ccfaf381bd28721b9998fcb.html>

 

<%@ 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>