gpt4 book ai didi

java - 在 SmartGwt 中创建一个 validator 来检查 String 是否是 Integer

转载 作者:行者123 更新时间:2023-12-02 04:58:07 36 4
gpt4 key购买 nike

我有一个 TextItem,我想在其上设置一个 Validator,以便它检查输入是否仅为整数。

TextItem textItem = new TextItem();
textItem.setValidator(myValidator)

我应该如何创建我的 myValidator 来检查这一点?我有:

myValidatorcv = new CustomValidator() {

@Override
protected boolean condition(Object value) {
if (Integer.parseInt(value);){
return true;
}else
return false;
}
}
};

这是最好的方法吗?另外我还需要捕获 validator 内的数字格式异常吗?

最佳答案

Is that the best way to do this?

也许吧。任何时候您可以利用框架的内置功能,这通常表明解决方案是好的。有些人可能会说异常驱动编程不是最好的方法,因为处理它们的成本很高,但这取决于您。

Also do I also need to catch the number format exception inside the validator?

是的,这就是您如何知道它是否失败的方法。

myValidatorcv = new CustomValidator() {

@Override
protected boolean condition(Object value) {
try {
Integer.parseInt(value.toString());
return true;
} catch (NumberFormatException) {
return false;
}
}
};

我并不完全熟悉这个框架,但是如果您可以利用泛型来确保您正在验证的对象是 IntegerString 而不是 Object,这样会更好。

关于java - 在 SmartGwt 中创建一个 validator 来检查 String 是否是 Integer,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28567654/

36 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com