EL自定义简单函数实例

xiaoxiao2021-02-28  81

一个EL自定义函数的使用,需要三个文件。

1.具体实现方法(该方法要求是静态static的):

package com.htxx.utils; public class ElUtil { public static String Cz2En(Integer i){ System.out.println("1"); if(i==null){ return null; } String result=""; //char content[] = new char[i.length()]; // message.getChars(0, message.length(), content, 0); //StringBuffer result = new StringBuffer(content.length + 50); //for (int i = 0; i < content.length; i++) { switch (i) { case 1: result="one"; break; case 2: result="two"; break; case 3: result="three"; break; case 4: result="four"; break; case 5: result="five"; break; case 6: result="six"; break; case 7: result="seven"; break; case 8: result="eight"; break; case 9: result="nine"; break; case 0: result="zero"; break; } System.out.println("1"); return result; } } 2.配置文件(放在WEB-INF文件夹下):

<?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 web-jsptaglibrary_2_0.xsd"> <tlib-version>1.0</tlib-version> <short-name>EL Function</short-name> <!-- 自定义EL函数库的引用URI, 在JSP页面中可以这样引用:<%@taglib uri="/ELFunction" prefix="fn" %> --> <uri>/elTld</uri> <!--<function>元素用于描述一个EL自定义函数 --> <function> <description>数字转英文</description> <!--<name>子元素用于指定EL自定义函数的名称--> <name>Cz2En</name> <!--<function-class>子元素用于指定完整的Java类名--> <function-class>com.htxx.utils.ElUtil</function-class> <!--<function-signature>子元素用于指定Java类中的静态方法的签名, 方法签名必须指明方法的返回值类型及各个参数的类型,各个参数之间用逗号分隔。--> <function-signature>java.lang.String Cz2En(java.lang.Integer)</function-signature> </function> </taglib> 3.具体jsp页面引用该自定义函数( 语法:${prefix:method(params)}):

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%> <%@ taglib prefix="d" uri="/elTld"%> <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %> <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <base href="<%=basePath %>"> <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no"> <title>elDemo</title> <link href="<%=basePath %>css/css1.css" rel="stylesheet" type="text/css"> <script type="text/javascript" src="<%=basePath %>js/jquery-1.8.3.min.js"></script> <script type="text/javascript"> $(function(){ }); </script> </head> <body> <% List<Integer> list=new ArrayList<Integer>(); list.add(1); list.add(2); list.add(3); list.add(4); list.add(5); list.add(6); list.add(7); list.add(8); list.add(9); list.add(0); request.setAttribute("list",list); %> <div class="container"> <c:if test="${empty list}"> <div class="cn28"> nonoono </div><!--cn28 end--> </c:if> <c:forEach items="${list}" var="item" > <div class="cn28"> <div id="id" >${d:Cz2En(item)}</div> </div><!--cn28 end--> </c:forEach> <div class="clear"></div> </div><!--container end--> </body> </html> 结果:

转载请注明原文地址: https://www.6miu.com/read-75190.html

最新回复(0)