简体   繁体   English

如何使用spring注释检查字符串是否为整数?

[英]how check string is integer or not by using spring annotations?

**i want test user input is a alpha-numeric or integer by using spring form annotations ** **我希望测试用户输入是使用弹簧形式注释的字母数字或整数**

 i have also tried this code. 
 @NotEmpty
        @NumberFormat(style = Style.NUMBER)
        @Length(min =11,max =11)
        @Column(name="abc")
        private String xyz;

I ran into a similar problem and used the @Pattern annotation to compare against a regular expression. 我遇到了类似的问题,并使用@Pattern注释与正则表达式进行比较。

@Pattern(regexp="^(0|[1-9][0-9]*)$")

The annotation is part of the javax.validation.constraints package. 注释是javax.validation.constraints包的一部分。

The @NumberFormat annotation only applies to subclasses of java.lang.Number ( Integer , Float , Double , BigDecimal , etc.); @NumberFormat 注释仅适用于java.lang.Number的子类( IntegerFloatDoubleBigDecimal等); therefore, it won't work with Strings. 因此,它不适用于字符串。 For this, you may have to create a custom annotation that validates the String only contains numbers. 为此,您可能必须创建一个自定义注释,以验证String仅包含数字。

Take a look at the following tutorial; 看看下面的教程; it's very thorough on how to create custom annotations and use them for validation with Spring: http://softwarecave.org/2014/03/27/custom-bean-validation-constraints/ 它非常全面地介绍如何创建自定义注释并使用它们进行Spring验证: http//softwarecave.org/2014/03/27/custom-bean-validation-constraints/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM