【问题标题】:How to pass a BigDecimal to a custom taglib (WebSphere)如何将 BigDecimal 传递给自定义标记库(WebSphere)
【发布时间】:2013-04-27 15:56:53
【问题描述】:

我有一个自定义标记库,我尝试将 BigDecimal 提供给它,但没有保留比例。似乎 BigDecimal 转换为双精度,然后又转换回 BigDecimal。这是 WebSphere 中的错误吗?还是我错过了什么?

我的控制器(我正在使用 Spring):

@Controller
@RequestMapping("/BigDecimalTest")
public class BigDecimalTestController {

    @RequestMapping
    public String display(final Map<String, Object> model) {
        model.put("bigDecimal", new BigDecimal("126.58"));
        return "bigDecimalTest";
    }

}

我的 JSP:

<%@taglib uri="/custom-taglibs/bigDecimal.tld" prefix="bd"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
        "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
  <title>BigDecimal Test</title>
</head>
<body>
<bd:display value="${bigDecimal}"/>
</body>
</html>

我的 taglib 描述符:

<taglib xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-jsptaglibrary_2_0.xsd"
    version="2.0">
  <description>BigDecimal test</description>
  <tlib-version>1.0</tlib-version>
  <short-name>bd</short-name>
  <tag>
    <description>Display informations about a BigDecimal</description>
    <name>display</name>
    <tag-class>BigDecimalDisplay</tag-class>
    <body-content>empty</body-content>
    <attribute>
      <name>value</name>
      <required>true</required>
      <rtexprvalue>true</rtexprvalue>
      <type>java.math.BigDecimal</type>
    </attribute>
  </tag>
</taglib>

我的标签类:

public class BigDecimalDisplay extends TagSupport {

    private static final long serialVersionUID = 2049900195699675393L;

    private BigDecimal value;

    public void setValue(final BigDecimal value) {
        this.value = value;
    }

    @Override
    public int doStartTag() throws JspException {
        final JspWriter out = pageContext.getOut();
        try {
            out.print("<div>");
            out.print("<div>Value: ");
            out.print(value.toPlainString());
            out.print("</div>");
            out.print("<div>Scale: ");
            out.print(value.scale());
            out.print("</div>");
            out.print("</div>");
        } catch (final IOException e) {
            throw new JspException(e);
        }
        return SKIP_BODY;
    }

}

预期输出:

价值:126.58
规模:2

实际输出:

值:126.5799999999999982946974341757595539093017578125
规模:46

编辑:我能够在不使用 Spring 的情况下重现此问题。由于 obourgain 无法在 Jetty 上重现它,现在看来它确实是一个 WebSphere 错误。

编辑:这可能是 IBM 错误 PM48569

【问题讨论】:

  • 我无法在我的应用程序中重现该错误。我创建了与您相同的标签,具有相同的 taglib 定义。该标签按预期显示“value 126.58”和“scale 2”。我的应用使用 Jetty 运行,不使用 Spring。
  • @obourgain 很有趣。所以这将是一个 WebSphere 或一个 Spring 错误。

标签: java jsp websphere taglib


【解决方案1】:

先生。 如果你知道你想要多少个小数,你可能可以制作类似模式的东西。 链接:http://docs.oracle.com/javase/1.4.2/docs/api/java/text/DecimalFormat.html 我希望这能帮到您。 格。吉莲

【讨论】:

  • 其实我不知道小数位数。比例设置在 BigDecimal 中,我想保留它。还是谢谢。
  • 那是一种混乱的方式。当然,按照您想要的方式转换值是没有问题的,问题是为什么会发生这种转换以及如何防止它发生。您正在与症状作斗争,而不是解决实际问题;)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2011-09-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多