f:convertNumber不会在十进制尾随字母字符上引发转换错误 [英] f:convertNumber doesn't throw conversion error on trailing alphabetic characters in decimal

查看:115
本文介绍了f:convertNumber不会在十进制尾随字母字符上引发转换错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用<f:convertNumber>标记转换十进制输入.

I am using <f:convertNumber> tag to convert a decimal input.

<f:convertNumber minFractionDigits="2" />

但是它接受结尾的字母字符.例如,如果我输入12345.1234AAA,它将转换为12345.123.我希望它引发转换错误,而不是修剪字母字符.我该如何实现?

But it's accepting trailing alphabatic characters. For example, if I input 12345.1234AAA it converts to 12345.123. I would like it to throw a conversion error on that instead of trimming the alphabetic characters. How can I achieve this?

推荐答案

这只是"> c3的标准行为 <f:convertNumber>所使用的> :它会修剪第一个不允许的字符之后的所有条目(例如,字母或小数点分隔符,而不是输入语言环境中定义的分隔符).仅当令人反感的字符位于开头时,转换才会失败.

This simply is the standard behavior of java.text.NumberFormat as used by <f:convertNumber>: It trims all entries after the first not-allowed character (e.g. letters or decimal separators others than the ones defined in the input locale). Conversion only fails if the offending character is at the beginning.

我不太喜欢这种行为,因为用户可能不会注意到自己错误输入的值被修剪掉了,从而导致意外的(甚至更糟糕的)自动校正为错误的值.

I don't really like this behaviour because a user may not notice his mis-typed value got trimmed, resulting in unintended and (even worse) unnoticed auto-correction into a wrong value.

有趣的是,特定数字转换器的行为有所不同.使用<f:converter converterId="javax.faces.Double" />不会自动处理非数字输入,但会导致转换失败.但是,标记不具有convertNumber的其他属性,例如minFractionDigits或货币符号.

Interestingly enough, the specific number converters behave differently. Using <f:converter converterId="javax.faces.Double" /> does not automatically handle non-numerical input, but fails conversion instead. However, the tag does not possess the additional attributes of convertNumber, such as minFractionDigits or currency symbols.

另一种方法是编写您自己的自定义转换器,该自定义转换器扩展了标准转换器,如在此答案中所述所述.然后,您可以检查字符值,如果发现任何字符,则直接中止进一步处理(引发相应的转换异常).如果该值已不含字符,则可以调用从其派生的转换器的标准行为.

A different approach would be to write your own custom converter which extends the standard converter as described in this answer. You could then check the value for characters and directly abort further processing (throwing an according conversion exception) if you find any. If the value is already character-free you could call the standard behavior of the converter you derive from.

两种方法的缺点是您失去了convertNumber的其他功能.您没有诸如<f:converter>上的minFractionDigitscurrencySymbol之类的属性,因此,如果需要此属性,则必须找到另一种传递参数的方法.我们的实现不需要任何这些,因此我没有进一步研究(我们采用了方法A),但是此答案提出了一种方法.

The drawback of both ways is that you loose the additional capabilities of convertNumber. You do not have attributes such as minFractionDigits or currencySymbol on <f:converter>, so if you need this you'd have to find another way to pass in a parameter. Our implementation does not require any of these, so I haven't looked into this any further (we went for approach A), but this answer presents an approach to do so.

这篇关于f:convertNumber不会在十进制尾随字母字符上引发转换错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆