4

----样本VO

@NumberFormat(pattern = "###,##0")
private int money=100000;

- - -控制器

@RequestMapping(value="/com/spelSample.do")
public String spelSample(SampleVO sampleVO,  Model model){

    model.addAttribute("sampleVO", sampleVO);

    return "sampleResult";
}

--------sampleResult.jsp

money: <spring:eval expression="sampleVO.money"/>

- - -期待

money : 100,000

------但是,结果是

money : 100000

问题是什么?我应该怎么办?

4

1 回答 1

5

文档_@NumberFormat

声明应将字段格式化为数字。支持按样式或自定义模式字符串格式化。可以应用于任何 JDK java.lang.Number 类型

您在原始领域使用它。显然这不包括在内。使用Integer而不是int.

编辑:更准确地说,并非所有可能的子类都java.lang.Number被涵盖。以下是相关摘录NumberFormatAnnotationFormatterFactory

public NumberFormatAnnotationFormatterFactory() {
    Set<Class<?>> rawFieldTypes = new HashSet<Class<?>>(7);
    rawFieldTypes.add(Short.class);
    rawFieldTypes.add(Integer.class);
    rawFieldTypes.add(Long.class);
    rawFieldTypes.add(Float.class);
    rawFieldTypes.add(Double.class);
    rawFieldTypes.add(BigDecimal.class);
    rawFieldTypes.add(BigInteger.class);
    this.fieldTypes = Collections.unmodifiableSet(rawFieldTypes);
}

这意味着缺少来自并发 api 的 Atomic* 类,以及来自 Commons/Lang 等框架的所有自定义 Number 实现。

更新:(见评论)您还需要添加<mvc:annotation-driven>到您的 context.xml。

于 2011-08-24T07:46:53.880 回答