我自己的 jsp 标签有问题。我希望它允许空值,但如果我想为我的处理程序提供空值,则该值为 0 而不是空值。
我的处理程序:
public class BigDecimalStripper extends SimpleTagSupport {
private BigDecimal numberToStrip;
public void setNumberToStrip(BigDecimal numberToStrip) {
this.numberToStrip = numberToStrip;
}
@Override
public void doTag() throws JspException, IOException {
if (numberToStrip == null) {
return;
}
JspWriter out = getJspContext().getOut();
BigDecimal withoutTrailingZeros = numberToStrip.stripTrailingZeros();
String formattedNumber = withoutTrailingZeros.toPlainString();
out.println(formattedNumber);
}
}
我的标签库:
<?xml version="1.0" encoding="UTF-8" ?>
<taglib>
<tlib-version>1.0</tlib-version>
<jsp-version>2.0</jsp-version>
<short-name>Fishie JSP Utils</short-name>
<tag>
<name>BigDecimalStrip</name>
<tag-class>ch.fishie.jsp.utils.BigDecimalStripper</tag-class>
<body-content>scriptless</body-content>
<attribute>
<name>numberToStrip</name>
<required>true</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
</tag>
我的 JSP 代码:
<c:forEach items="${pagination.pageEntries}" var="aquarium">
<tr>
<td class="name"><c:out value="${aquarium.name}" /></td>
<td class="length"><fu:BigDecimalStrip numberToStrip="${aquarium.length}" /></td>
.....
aquarium.length 为空,但是当它设置为 时BigDecimalStripper
,它是 0。有人看到我犯的错误吗?