【问题标题】:SpEL (@NumberFormat) is not workingSpEL (@NumberFormat) 不工作
【发布时间】:2011-08-24 07:27:16
【问题描述】:

----SampleVO

@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

有什么问题? 我该怎么办?

【问题讨论】:

    标签: java spring spring-mvc annotations spring-el


    【解决方案1】:

    来自the @NumberFormat docs

    声明字段应格式化为数字。支持 按样式或自定义模式字符串格式化。 可以应用于任何 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 实现。

    更新:(参见 cmets)您还需要将 &lt;mvc:annotation-driven&gt; 添加到您的 context.xml。

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-12-23
    • 1970-01-01
    • 1970-01-01
    • 2013-09-07
    • 1970-01-01
    • 2016-03-18
    相关资源
    最近更新 更多