Spring 3 MVC: Show validation message with custom validator

Spread the love

Question Description

I need help. I am beginner in jsp, MVC. I want to validate form input with custom validator in Spring 3 MVC.

My validator class

   package validators;

import models.UserModel;

import org.springframework.stereotype.Component;
import org.springframework.validation.Errors;
import org.springframework.validation.ValidationUtils;
import org.springframework.validation.Validator;
@Component
public class UserValidator implements Validator {

   @Override
   public boolean supports(Class clazz) {
      return UserModel.class.isAssignableFrom(clazz);
   }

   @Override
   public void validate(Object target, Errors errors) {
      ValidationUtils.rejectIfEmptyOrWhitespace(errors, "firstname", "Enter firstname.");
      ValidationUtils.rejectIfEmptyOrWhitespace(errors, "surname", "Enter surname.");
      ValidationUtils.rejectIfEmptyOrWhitespace(errors, "login", "Enter login.");

   }

}

Controller class

package controllers;

import java.util.ArrayList;
import models.UserModel;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;
import validators.UserValidator;
import database.UserDB;


@Controller
public class UserController {

@RequestMapping(value="pouzivatel/new", method=RequestMethod.POST)
   public ModelAndView newUser(@ModelAttribute UserModel user, BindingResult result){
      UserValidator validator = new UserValidator();
      validator.validate(user, result);
      if(result.hasErrors()){
         return new ModelAndView("/user/new","command",user);

      }
      ...
}

Model for User

package models;

public class UserModel  {
   private String firstname="";
   private String surname="";

   public String getFirstname() {
      return firstname;
   }
   public String getSurname() {
      return surname;
   }

   public void setFirstname(String firstname) {
      this.firstname = firstname;
   }
   public void setSurname(String surname) {
      this.surname = surname;
   }

}

JSP veiw new.jsp which is in directory /web-inf/user (it just only form)


            
FirstName
Surname

dispatcher servlet.xml




   
    

   
      
      
      
   


Problem is a display validation message in view. Validation is successful and in variable resut (BindingResult) are errors. Controller return follow part of code

if(result.hasErrors()){
         return new ModelAndView("/user/new","command",user);

Another way is use Annotation validation (I preffer custom validator), but why i can not see validation messages on view, when input fields are empty.

Can you give me example how to do it right?

Thanks for reply.

Practice As Follows

This happens because of mismatch between default model attribute names in view and controller:

  • When you write without modelAttribute (or commandName) attribute, it uses default model attribute name command.
  • When you write @ModelAttribute UserModel user in your controller, it assumes that the name of this attribute is a decapitalized class name, i.e. userModel.

That is, error messages produced by validator are bound to model attribute named userModel, while your view tries to show errors for model attribute command.

You need to set a model attribute name explicitly, either in the view () or in the controller (@ModelAttribute("command")).

Leave a Comment