自定义EL表达式的创建和使用
时间:2010-07-18 来源:yanlei3032
EL表达式是J2EE编程中经常用到的东西,所以今天特意按照书籍写了一遍,希望对己对别人有用,首先是JAVA方法的定义
类名是public的,方法定义为public static
package www.el; import java.math.BigDecimal; import java.math.MathContext; public class Common { //定义精度值 public static final MathContext math = new MathContext(2); public static Double add(Double a, Double c) { BigDecimal ba = new BigDecimal(a, math); BigDecimal bc = new BigDecimal(c, math); ba = ba.add(bc); return ba.doubleValue(); } }
tld配置文件的配置--》放置于和WEB-INF同级的位置:
<?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 web-jsptaglibrary_2_0.xsd" version="2.0"> <tlib-version>1.0</tlib-version> <sort-name>common</sort-name> <uri>/common</uri> <function> <description>add x and y</description> <name>add</name> <function-class>www.el.Common</function-class> <function-signature> double add(java.lang.Double,java.lang.Double) </function-signature> </function> </taglib>
jsp中的调用:
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@taglib uri="/WEB-INF/common.tld" prefix="com"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Insert title here</title> </head> <body> ${com:add(2.1,5.2)} </body> </html>
相关阅读 更多 +