SpringMVC Ajax Spring Check

I have a Spring Mvc 3 form, which is POST for the controller, in the controller I make calls to execute DML statements. I also have a separate validation class that implements Validator and is called in my controller. I perform a simple as well as a complex check, for example, checks if the username exists and returns an error message if it exists.

I would like to use ajax to validate only the username field when I exit the field in the view, but I would like to make a call to the already implemented validation that I have and only validate the username.

How can I achieve this, I need to make an Ajax call for the Controller class and have a separate @RequestMapping to handle this only? Can I give an example of how this should be implemented.

+4
source share
2 answers

I read a very good tutorial article on this issue on the SpringFramework website.

Please refer to this link . And this is the link .

Hope this helps you. Greetings.

+8
source

// add this on the controller side

if(result.hasErrors() ){ for (Object object : result.getAllErrors()) { if(object instanceof FieldError) { FieldError fieldError = (FieldError) object; String message = messageSource.getMessage(fieldError, null); logger.info("Error : " + message + " - " + fieldError.getField()); errors.add(fieldError.getField() + "#" + message); } } response.setStatus("FAIL"); response.setResult(errors); 

after ajax answer, set the error in the corresponding result fields

 $.each(response.result, function(index, errorString){ var array = errorString.split("#"); $('[for="' +array[0]+ '"]').html(array[1]); }); 
+1
source

Source: https://habr.com/ru/post/1434496/


All Articles