自定义 jsp tag
该taglib解决了jsp fmt:formatNumber 四舍五入 奇入偶不入的问题
1先写一个tld
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE taglib PUBLIC "-//Sun Microsystems, Inc.//DTD JSP Tag Library 1.2//EN"
"http://java.sun.com/dtd/web-jsptaglibrary_1_2.dtd">
<taglib>
<tlib-version>1.0</tlib-version>
<jsp-version>1.2</jsp-version>
<short-name>holyant</short-name>
<uri>http://www.holyant.com/jsp</uri>
<tag>
<name>NormalRound</name>
<tag-class>com.junf.bbd.common.taglibs.HolyantTag</tag-class>
<attribute>
<name>value</name>
<required>true</required>
</attribute>
<attribute>
<name>scale</name>
<required>true</required>
</attribute>
</tag>
</taglib>
2.web.xml 配置
<jsp-config>
<taglib>
<taglib-uri>http://www.holyant.com/jsp</taglib-uri>
<taglib-location>/WEB-INF/tlds/holyant.tld</taglib-location>
</taglib>
</jsp-config>
3. 创建class HolyantTag
package com.junf.bbd.common.taglibs;
import org.apache.taglibs.standard.tag.el.fmt.FormatNumberTag;
import javax.servlet.jsp.JspException;
import java.math.BigDecimal;
public class HolyantTag extends FormatNumberTag {
private static final long serialVersionUID = 1L;
private int scale;
@Override
public int doEndTag() throws JspException {
BigDecimal v = new BigDecimal(value.toString());
try {
pageContext.getOut().print(v.setScale(scale, BigDecimal.ROUND_HALF_UP).toString());
} catch (Exception e) {
throw new JspException(e.getMessage());
}
return EVAL_PAGE;
}
public int getScale() {
return scale;
}
public void setScale(int scale) {
this.scale = scale;
}
}
4. jsp中使用
<%@taglib prefix="holyant" uri="http://www.holyant.com/jsp"%>
<holyant:NormalRound value="0.105" scale="2"/>