Need <f:convertNumber> to throw error when fractions or separator characters are used

我的梦境 提交于 2019-12-02 06:05:58

Create a custom converter extending the default NumberConverter wherein you check the string value before delegating to the NumberConverter and then use it instead.

public class MyNumberConverter extends NumberConverter {

    public MyNumberConverter() {
        setIntegerOnly(true);
    }

    @Override
    public Object getAsObject(FacesContext context, UIComponent component, String submittedValue) {
        if (submittedValue != null && !submittedValue.matches("[0-9]+")) {
            throw new ConverterException("Please enter digits only");
        }

        return super.getAsObject(context, component, submittedValue);
    }

}
<f:converter converterId="myNumberConverter" />
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!