自定义EL表达式函数

自定义EL表达式函数

JSTL 可以自己扩展,EL也是可以扩展的

1) 编写函数
package com.wicresoft.jpo.demo.el;

import java.util.Collection;

public class ELFunctions {

	// 函数必须是 public static 修饰
	public static boolean contains(@SuppressWarnings("rawtypes") Collection collection, Object test) {
		if (collection == null || collection.isEmpty()) return false;
		return collection.contains(test);
	}

}


2) el.tld
<?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>myFunctions library</description>
	<display-name>JSTL functions</display-name>
	<tlib-version>1.1</tlib-version>

	<short-name>mfn</short-name>
	<uri>http://www.wicresoft.com/jsp/functions</uri>

	<function>
		<name>contains</name>
		<function-class>com.wicresoft.jpo.demo.el.ELFunctions</function-class>
		<function-signature>boolean contains(java.util.Collection,java.lang.Object)</function-signature>
	</function>
</taglib>


3) el.tld 放在/WEB-INF/ 下即可

4) JSP 页面
<%@ taglib prefix="mfn" uri="http://www.wicresoft.com/jsp/functions" %>

${mfn:contains(form.xxx, yyy)

猜你喜欢

转载自yingzhuo.iteye.com/blog/1453624