简体   繁体   English

JSR303注释@pattern不起作用

[英]JSR303 annotation @pattern is not working

Im using a JSR303 annotation @Pattern on a field to check for String that contains whitespaces only. 我在字段上使用JSR303注释@Pattern来检查仅包含空格的String。 If whitespaces only are found then throw a message however the @Pattern is not working 如果仅找到空格,则引发一条消息,但是@Pattern无法正常工作

@Length(max = AlertConstants.LENGTH_FOR_TWITTER_TEXTS, message = "{twitter.message.max}")
@Pattern(regexp ="^\\s*$", message = "{message.twitter.content.not.empty}")
private String twitterChannelContent = "";

im using @length as above which is working fine. 即时通讯使用@length上面的工作正常。 Please suggest 请建议

There are two possibilities: 有两种可能性:

  • Either you misunderstand the function of the "^" character, it performs a "not" when enclosed in brackets, however as you have written it, it indicates the start of the string. 您可能误解了“ ^”字符的功能,将其括在方括号中时,它会执行“ not”,但是,如您所写,它指示了字符串的开头。

  • Or you mistunderstand the function of the Pattern annotation. 否则您会误解Pattern批注的功能。 It will validate that the string matches the given pattern and if not, it uses the error message. 它将验证字符串是否匹配给定的模式,如果不匹配,则使用错误消息。

Either way, currently you are saying that the string should only contain whitespaces. 无论哪种方式,当前您都在说字符串应仅包含空格。

Allow space back and forth : ([^^$]|^\\s*\\S+\\s*$) 允许来回空间: ([^^ $] | ^ \\ s * \\ S + \\ s * $)

No spaces : ([^^$]|^\\S+$) 无空格: ([^^ $] | ^ \\ S + $)

Troubleshooting point : [^ WOW ] - Denied "WOW" 故障排除点:[^ WOW ]-拒绝“ WOW”

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

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