【发布时间】:2013-03-22 19:21:01
【问题描述】:
我自己的 jsp 标签有问题。我希望它允许 null 值,但如果我想给我的处理程序提供 null 值,则该值为 0 而不是 null。
我的处理程序:
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。
有人看到我犯的错误吗?
【问题讨论】: