JAVA
package com.practice.tld;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.TagSupport;
import java.io.IOException;
import java.text.DecimalFormat;
public class sfmt extends TagSupport {
private String sfmt;
@Override
public int doStartTag() throws JspException {
DecimalFormat df = new DecimalFormat("###,###.###");
String format = df.format(Double.parseDouble(sfmt));
try {
pageContext.getOut().write(format);
} catch (IOException e) {
e.printStackTrace();
}
return super.doStartTag();
}
public String getSfmt() {
return sfmt;
}
public void setSfmt(String sfmt) {
this.sfmt = sfmt;
}
}
TLD
<?xml version="1.0" encoding="utf-8"?>
<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">
<!--版本号-->
<tlib-version>1.0</tlib-version>
<!--名字-->
<short-name>stringFormat</short-name>
<!-- Invoke 'Generate' action to add tags or functions -->
<tag>
<!--引入名字 sf:名字-->
<name>string</name>
<!--处理类路径-->
<tag-class>com.practice.tld.sfmt</tag-class>
<body-content>JSP</body-content>
<attribute>
<!--参数名称 对应处理类的变量名-->
<name>sfmt</name>
<!--是否必填-->
<required>true</required>
<!--是否支持运算-->
<rtexprvalue>true</rtexprvalue>
</attribute>
</tag>
</taglib>
JSP
<%@ page contentType="text/html;charset=UTF-8" pageEncoding="UTF-8" %>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<meta name="description" content="">
<meta name="keywords" content="">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<title>pay</title>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%@ taglib uri="/WEB-INF/tld/sfmt.tld" prefix="sf" %>
</head>
<body>
<sf:string sfmt="${abc}"></sf:string>
</body>
</html>
依赖
<dependency>
<groupId>jstl</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
<dependency>
<groupId>taglibs</groupId>
<artifactId>standard</artifactId>
<version>1.1.2</version>
</dependency>