Need to throw error when fractions or separator characters are used

前端 未结 1 1019
别那么骄傲
别那么骄傲 2021-01-25 00:38

I have an InputText component wired to a Bean property of int type. However, I\'m forced to use NumberConverter only

Even when I specify

相关标签:
1条回答
  • 2021-01-25 01:22

    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" />
    
    0 讨论(0)
提交回复
热议问题