【问题标题】:JSP custom tags null valueJSP自定义标签空值
【发布时间】: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。 有人看到我犯的错误吗?

【问题讨论】:

    标签: jsp taglib


    【解决方案1】:

    Tomcat 内置的 EL 解析器将为所有扩展 Number 的类执行此操作。 您必须使用 Tomcat 6.0.16 或更高版本,试试这个...

    -Dorg.apache.el.parser.COERCE_TO_ZERO=false

    【讨论】:

      【解决方案2】:

      接受的答案中的 COERCE_TO_ZERO 参数对我的设置没有任何改变(Tomcat 7.0.40、Liferay 6.1.2),但博客 How To Pass A Null Value To A Custom Tag Library 技巧确实如此。

      在标签属性设置器参数 (setNumberToStrip) 中将 BigDecimal 替换为 Object 并在存储到字段中之前转换为 BigDecimal。如有必要,执行 instanceofnull 检查。

      public void setNumberToStrip(Object numberToStrip) {
          if (value instanceof BigDecimal || value == null) {
              this.numberToStrip = (BigDecimal) numberToStrip;
          }
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2014-05-11
        • 1970-01-01
        • 2014-01-05
        • 2014-11-16
        • 2011-04-19
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多