Use Bean Validation API for validate object's for save in DB by Hibernate. With english letters all fine:
@Pattern(regexp="^[a-zA-Z]+$",message="Имя автора только из букв")
private String name;
When i wrote this:
@Pattern(regexp="^[a-zа-яA-ZА-Я]+$", message="Имя автора только из букв")
private String name;
It's doesn't work, take error about wrong enter data (Имя автора только из букв)
But how add russian letters in regexp?
Yes, problem in Spring form. When remove regexp and enter russian text, it's write in database something like this ÐеÑÑов
The Bean Validation API provides a set of built-in constraints and an interface that enables you to declare custom constraints. This is accomplished by creating constraint annotations and declaring an annotation on a bean type, field, or property.
Very basically, Bean Validation works by defining constraints to the fields of a class by annotating them with certain annotations.
Bean Validation or commonly known as JSR-380 is a Java standard that is used to perform validation in Java applications. To perform validation, data Items are applied constraints. As long as the data satisfies these constraints, it will be considered valid.
The Bean Validation API is a Java specification which is used to apply constraints on object model via annotations. Here, we can validate a length, number, regular expression, etc. Apart from that, we can also provide custom validations. As Bean Validation API is just a specification, it requires an implementation.
Problem solved by add this in web-app context:
<filter>
<filter-name>encodingFilter</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
<init-param>
<param-name>forceEncoding</param-name>
<param-value>true</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>encodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
and regexp like [a-zA-Zа-яА-Я]
work fine.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With