Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Bean Validation API

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 ÐеÑÑов

like image 807
disable1992 Avatar asked Apr 02 '14 12:04

disable1992


People also ask

What is Bean Validation API?

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.

How does Bean Validation work?

Very basically, Bean Validation works by defining constraints to the fields of a class by annotating them with certain annotations.

What is Bean Validation in spring boot?

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.

How does Java Bean validation work?

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.


1 Answers

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.

like image 65
disable1992 Avatar answered Oct 21 '22 16:10

disable1992