Resolve issue on white space in validation(Spring validation)




In the above form we need to fill all required fields to submit the form. In some case we able to by pass the form by using white spaces. But the application could not able to allow the user to submit with white space.

For resolve this issue we can use @InitBinder in spring. This @InitBinder will


  • Pre-process every string form data 
  • Remove leading and trailing white space
  • If string only has white space ... trim it to null


For use this @InitBinder we need to initBinder method in the Controller.

@InitBinder
public void initBinder(WebDataBinder dataBinder) {

StringTrimmerEditor stringTrimmerEditor = new StringTrimmerEditor(true);
dataBinder.registerCustomEditor(String.class, stringTrimmerEditor);

}

By using this we can resolve the issue



Comments

Popular posts from this blog

Nginx

Use @Initbinder in Spring MVC

Different ways to Authenticate a Web Application

AWS Configuration For RDS(postgres),ElastiCache(Redis) with ElasticBean

The TRUE difference between [] and {{}} bindings in Angular

Use of DispatcherServlet in Spring MVC Framework

CSRF Protection using Synchronizer Tokens

CSRF Protection using Double Submitted Cookies

Agile Methodology – Know the What and How?

Why you should use BCrypt to hash passwords