jsp自定义函数标签

以自定义数字转汉字为例:

1.新建一个包,再包下建一个类



	// 
    //  将传入的数字字符串4位一段变成integer 数组
    //  方法名:tolistfour1
    //  创建人:jcy
    //  时间:2018年5月27日-下午5:51:30 
    //  手机:13199657597
    //  @param string
    //  @return Integer[]
    //  @exception 
    //  @since  1.0.0
    // 
public static Integer[] tolistfour1(String string){
		int start = -4;
		int end = 0;
		String str = "";
		Integer strnum = 0;
		String string2 = tofour(string);
		int num = string2.length();
		num = num/4;
		//System.out.println(num);
		Integer[] arrStrings = new Integer[num];
		for(int i=0;i<num;i++){
			start+=4;
			end+=4;
			str = string2.substring(start,end);
			strnum = Integer.valueOf(str);
			//System.out.println(strnum);
			arrStrings[i] = strnum;
		}
		return arrStrings;
	};
	
        /**
         * 
         * 将数组变成汉字输出
         * 方法名:numtoString
         * 创建人:jcy 
         * 时间:2018年5月27日-下午5:58:28 
         * 手机:13199657597
         * @param string1s
         * @return String
         * @exception 
         * @since  1.0.0
         */
	public static String numtoString (String string1s){
		
		// num 表示数字,lower表示小写,upper表示大写
		final String[] num_lower = { "零", "一", "二", "三", "四", "五",
				"六", "七", "八", "九" };
		final String[] num_upper = { "零", "壹", "贰", "叁", "肆", "伍",
				"陆", "柒", "捌", "玖" };


		// unit 表示单位权值,lower表示小写,upper表示大写
		final String[] unit_lower = { "", "十", "百", "千" };
		final String[] unit_upper = { "", "拾", "佰", "仟" };
		final String[] unit_common = { "","万", "亿",  "兆", "京", "垓",
				"秭", "穰", "沟", "涧", "正", "载" };
		Integer[] integers = tolistfour1(string1s);
		Integer j = integers.length-1;
		String string = "";
		for (int i = 0; i <integers.length; i++) {
			Integer integer = integers[i];
			String str  = integer.toString();
			Integer th = integer/1000;
			Integer h = integer/100%10;
			Integer ten = integer%100/10;
			Integer one = integer%100%10;
			
			String ths="",hs="",tens="",ones="";
			if (th!=0) {
				ths = num_lower[th]+ unit_lower[3];
				
			}
			if (h!=0) {
				hs = num_lower[h]+ unit_lower[2];
			}
			if (ten!=0) {
				tens = num_lower[ten]+ unit_lower[1];
				
			}
			if (one!=0) {
				ones = num_lower[one]+ unit_lower[0];
			}
				
			String string1 = ths+hs+tens+ones+unit_common[j];
			j -=1;
			 string=string + string1;
		}
		return string;
	}

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">
<description>jcy的自定义标签</description>
  <display-name>JSTL functions</display-name>
  <tlib-version>1.0</tlib-version>
  <!-- 自定义标签的前缀 -->
  <short-name>jcy</short-name>
  <!-- 自定义标签的uri 采用的是1.2以上的版本不用配置-->
  <uri></uri>
<!-- 函数标签 -->
  <function>
    <description>
     	数字转换成汉字
    </description>
    <!--标签的名称  -->
    <name>numtoString</name>
    <!-- 标签对应的类 -->
    <function-class>com.array.tag.NumToString</function-class>
    <!--类中对应的方法  -->
    <function-signature>String numtoString(java.lang.String)</function-signature>
    
    <example>
      ${jcy:numtoString("4445211895")}
    </example>
  </function>
</taglib>

3.在jsp页面中调用:

<%@ taglib uri="WEB-INF/mytag1.tld" prefix="jcy" %>

这里的uri如果采用的是1.2版本以上的话不用配置,直接导入tld的路径

如果配置就要与tld中的uri保持一致

445117899==${jcy:numtoString("445117899") }

运行结果:445117899==四亿四千五百一十一万七千八百九十九



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值