<?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>JSTL_game_EXT_FUNCTION</description>
<tlib-version>2.0</tlib-version>
<short-name>xfn</short-name>
<uri>http://www.game.com/gameFunction</uri>
<!-- 替换序号为A/B/C/D/.... -->
<function>
<description>substring</description>
<name>replaceOptions</name>
<function-class>com.game.common.utils.gameFnTag</function-class>
<function-signature>java.lang.String replaceOptions(java.lang.Integer)</function-signature>
<example>${xfn:replaceOptions(score)}</example>
</function>
<!-- 截取字符串的tag -->
<function>
<description>substring</description>
<name>subStr</name>
<function-class>com.game.common.utils.gameFnTag</function-class>
<function-signature>java.lang.String subStr(java.lang.String,int,int)</function-signature>
<example>${xfn:subStr(str,start,end)}</example>
</function>
<function>
<description>substring</description>
<name>subStrs</name>
<function-class>com.game.common.utils.gameFnTag</function-class>
<function-signature>java.lang.String subStrs(java.lang.String,int,int,int,int)</function-signature>
<example>${xfn:subStrs(str,start,end,len,num)}</example>
</function>
<!-- 解析付费比率 -->
<function>
<description>substring</description>
<name>analyticPay</name>
<function-class>com.game.common.utils.gameFnTag</function-class>
<function-signature>java.lang.String analyticPay(int, int)</function-signature>
<example>${xfn:analyticPay(chargeNum, regNum)}</example>
</function>
<!-- 转换字符串日期显示格式 -->
<function>
<description>substring</description>
<name>dataStrToStr</name>
<function-class>com.game.common.utils.gameFnTag</function-class>
<function-signature>java.lang.String dataStrToStr(java.lang.String, java.lang.String, java.lang.String)</function-signature>
<example>${xfn:dataStrToStr(chargeNum, regNum)}</example>
</function>
<!-- 除法运算 -->
<function>
<description>division</description>
<name>division</name>
<function-class>com.game.common.utils.gameFnTag</function-class>
<function-signature>java.lang.String division(int, int)</function-signature>
<example>${xfn:division(member, denominator)}</example>
</function>
<!-- 格式化金额 -->
<function>
<description>formatDoubleToMoney</description>
<name>formatDoubleToMoney</name>
<function-class>com.game.common.utils.gameFnTag</function-class>
<function-signature>java.lang.String formatDoubleToMoney(java.lang.Double)</function-signature>
<example>${xfn:formatDoubleToMoney(money)}</example>
</function>
<function>
<description>formatIntegerToMoney</description>
<name>formatIntegerToMoney</name>
<function-class>com.game.common.utils.gameFnTag</function-class>
<function-signature>java.lang.String formatIntegerToMoney(java.lang.Integer)</function-signature>
<example>${xfn:formatIntegerToMoney(money)}</example>
</function>
</taglib>
数据字典:
<taglib xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-jsptaglibrary_2_1.xsd"
version="2.1">
<description>datadict core library</description>
<display-name>datadict core</display-name>
<tlib-version>1.0</tlib-version>
<uri>http://www.game.com/datadict</uri>
<tag>
<name>select</name>
<tag-class>com.game.common.utils.DataDictionarySupport</tag-class>
<body-content>empty</body-content>
<attribute>
<name>id</name>
<type>String</type>
<required>false</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
<attribute>
<name>name</name>
<type>String</type>
<required>true</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
<attribute>
<name>kindId</name>
<type>Integer</type>
<required>false</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
<attribute>
<name>depth</name>
<type>Integer</type>
<required>false</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
<attribute>
<name>pId</name>
<type>Integer</type>
<required>false</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
<attribute>
<name>style</name>
<type>String</type>
<required>false</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
<attribute>
<name>className</name>
<type>String</type>
<required>false</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
<attribute>
<name>defaultValue</name>
<required>false</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
<attribute>
<name>ltype</name>
<type>String</type>
<required>false</required>
<rtexprvalue>false</rtexprvalue>
</attribute>
<attribute>
<name>ltype</name>
<type>String</type>
<required>false</required>
<rtexprvalue>false</rtexprvalue>
</attribute>
<attribute>
<name>initId</name>
<type>String</type>
<required>false</required>
<rtexprvalue>false</rtexprvalue>
</attribute>
<attribute>
<name>initValue</name>
<type>String</type>
<required>false</required>
<rtexprvalue>false</rtexprvalue>
</attribute>
<attribute>
<name>hideValue</name>
<type>String</type>
<required>false</required>
<rtexprvalue>false</rtexprvalue>
</attribute>
<attribute>
<name>setOptValueFlag</name>
<type>String</type>
<required>false</required>
<rtexprvalue>false</rtexprvalue>
</attribute>
</tag>
</taglib>
Java funciton 代码
package com.exam.util;
import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.util.Date;
public class FnTag {
/**
* 截取字符串
* @param str 要截取的字符串
* @param start 起始位置(0开始,不包含)
* @param end 结束位置(包含)
* @return
*/
public static String subStr(String str, int start, int end) {
if(null == str)
return "";
if (str.trim().length() > end) {
return str.trim().substring(start, end);
} else {
return str.trim();
}
}
/**
* 截取字符串
* @param str 要截取的字符串
* @param start 起始位置(0开始,不包含)
* @param end 结束位置(包含)
* @param len 超过多少长度开始截取
* @param num 显示多少个省略...
* @return
*/
public static String subStrs(String str, int start, int end, int len, int num) {
StringBuilder strs = new StringBuilder();
if(null == str)
return "";
if (str.trim().length() > len) {
strs.append(str.trim().substring(start, end));
while (num>0) {
strs.append(".");
num--;
}
} else {
strs.append(str.trim());
}
return strs.toString();
}
/**
* 转换序号为字母A/B/C/D/....
* @param num 数字
* @return
*/
public static String replaceOptions(Integer num) {
if(num==null || num<0 || num>26) num = 1;
String options = ((char)(num+64))+"";
return options;
}
/**
* 解析付费率
* @param chargeNum 充值人数
* @param regNum 注册人数
* @return 格式:78.00%
*/
public static String analyticPay(int chargeNum, int regNum){
if(regNum == 0)
return "0.0%";
String value = "";
float f = (float)chargeNum / (float)regNum;
value = (Math.round(f*10000) / (float)100) + "%";
/**
if(f < 1.0f){
value = (Math.round(f*10000) / (float)100) + "%";
}else{
value = (Math.round(f*100) / (float)100) + "%";
}
**/
return value;
}
/**
* 除法运算
* @param member 分子
* @param denominator 分母
* @return
*/
public static String division(int member,int denominator){
if(denominator>0){
double d = (double)member/(double)denominator;
BigDecimal bd = new BigDecimal(d);
BigDecimal bd2 = bd.setScale(2, BigDecimal.ROUND_HALF_UP);
return bd2+"";
}
return "0";
}
/**
* 转换字符串日期显示格式
* @param data 字符串日期数据
* @param formatData 日期格式
* @param formatStr 最终显示格式
* @return
*/
public static String dataStrToStr(String data, String formatData, String formatStr) {
String str = "";
try {
Date date = StrUtil.getStrByDataTime(data, formatData);
str = StrUtil.getDateTimeByStr(date, formatStr);
} catch (RuntimeException e) {
e.printStackTrace();
}
return str;
}
/**
* 2个数相差多少
* @param A 数字
* @param B 数字
* @return
*/
public static boolean differ(double A,double B){
boolean fle=true;
double num=0;
if(A>B){
num=A-B;
}else{
num=B-A;
}
if(num>1000)
fle=false;
return fle;
}
/**
* 格式化金额
*/
public static String formatDoubleToMoney(Double number){
DecimalFormat format = new DecimalFormat();
format.applyPattern("##,###0.00");
return null==number?"0.00":format.format(number);
}
public static String formatIntegerToMoney(Integer number){
DecimalFormat format = new DecimalFormat();
format.applyPattern("##,###");
return null==number?"0":format.format(number);
}
public static void main(String[] args) {
// double value = (double)1/(double)3;
// BigDecimal bd = new BigDecimal(value);
// BigDecimal bd2 = bd.setScale(2, BigDecimal.ROUND_HALF_UP);
// System.out.println(bd2);
// System.out.println(division(1,3));
System.out.println(formatDoubleToMoney(0.031));
}
}
//注意 java 代码中的方法一定要为 静态的 ,否则会报空指针错误