项目场景:JSP页面,类似jstl的function fn:库中没有适合的功能方法,需要自定义。
本文示例JAVA类中实现根据正则表达式检测目标字符串返回真假的功能,JSP页面中实现根据文件名的后缀名,显示对应的样式。
//JAVA类
package com.weigs.util;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class FunctionTag {
// 必须是静态方法
public static Boolean regExpTest(String regExp,String distStr){
Pattern pattern = Pattern.compile(regExp);
Matcher matcher = pattern.matcher(distStr);
return matcher.find();
}
}
//xxx.dtl文件
1.0
customfn
http://com.weigs.util/custom/functiontags
regExpTest
com.qhgrain.app.gway.util.FunctionTag
java.lang.Boolean regExpTest(java.lang.String,java.lang.String)
//JSP页面
<%@ taglib prefix="customfn" uri="http://com.weigs.util/custom/functiontags"%>
注意:调用方法的时候必须类型要传入相同类型的值,否则的话会报错,不过对于方法体是String的话,是可以传入list,set,map那些,因为传入后会直接调用list的.toString()方法直接当字符串输出。