标签:自定义jstl函数标签
写个工具类
public class UtilFunction {
public UtilFunction() {
}
/**
* @param start 0,start 截取
* @param end end, 截取
* @param info
* @param t 填充
* @return
*/
public static String subString(int start,int end,String info,String t){
String infostart=info.substring(0,start);
String infoend=info.substring(end);
return infostart+t+infoend;
}
//测试
public static void main(String[] args) {
//手机号前3后4
System.out.println(UtilFunction.subString(3, 7, "15216888619","*****"));
}
}
2.在web-inf下面定义一个.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">
<tlib-version>1.0</tlib-version>
<short-name>my</short-name>
<uri>http://www.xx.com</uri>
<!-- JSTL自定义函数 获取省份 -->
<function>
<name>subString</name>
<function-class>util.UtilFunction</function-class>
<function-signature>java.util.String subString(int, int,java.lang.String,java.lang.String)</function-signature>
</function>
</taglib>
3.web.xml定义
<jsp-config>
<taglib>
<taglib-uri>http://www.xx.com</taglib-uri>
<taglib-location>/WEB-INF/tld/utilFunction.tld</taglib-location>
</taglib>
</jsp-config>
4.jsp页面引用
<%@ taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn"%>
<%@ taglib prefix="yh" uri="http://www.xx.com" %>
${xx:subString(3, 7, userinfo.sMobile,"****")}
注:
userinfo为request.setBean()
request绑定为${userinfo.sMobile} 如果fn函数使用去除${},直接userinfo.sMobile
标签:自定义jstl函数标签
原文地址:http://xuliangjun.blog.51cto.com/7398089/1720663