title: Validation not working on spring boot and hibernate
body: <p>I am new to spring boot and hibernate.I have declared a model class Office which is :</p>
  
  <pre><code>package com.ashwin.officeproject.model;
  import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
  import org.springframework.data.annotation.CreatedDate;
  import org.springframework.data.annotation.LastModifiedDate;
  import org.springframework.data.jpa.domain.support.AuditingEntityListener;
  import javax.persistence.*;
  import javax.validation.constraints.NotBlank;
  import javax.validation.constraints.NotEmpty;
  import javax.validation.constraints.NotNull;
  import javax.validation.constraints.Size;
  
  import java.util.Date;
  
  @Entity
  @Table(name = "office")
  public class Office {
  
      @Id
      @GeneratedValue(strategy = GenerationType.IDENTITY)
      private Long officeId;
  
      @NotEmpty(message = "{officename.notempty}")
      private String officeName;
  
      @NotNull
      private int officeNumber;
  
      /*@Size(min = 8, max = 72, message = "Your offc address between 8 and 72 characters long")*/
      /*@NotEmpty(message = "Please provide a offc name")*/
      private String officeAddress;
  
      public Office() {
  
      }
  
      //ommitted getters and setters
  }
  </code></pre>
  
  <p>I have declared my first main starting class as:</p>
  
  <pre><code>package com.ashwin.officeproject;
  
  import org.springframework.boot.SpringApplication;
  import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
  import org.springframework.boot.autoconfigure.SpringBootApplication;
  import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
  import org.springframework.context.MessageSource;
  import org.springframework.context.annotation.Bean;
  import org.springframework.context.support.ReloadableResourceBundleMessageSource;
  import org.springframework.validation.beanvalidation.LocalValidatorFactoryBean;
  
  @SpringBootApplication
  public class OfficeProjectApplication {
  
      public static void main(String[] args) {
          SpringApplication.run(OfficeProjectApplication.class, args);
      }
  
      @Bean
      public MessageSource messageSource() {
          ReloadableResourceBundleMessageSource messageSource
            = new ReloadableResourceBundleMessageSource();
  
          messageSource.setBasename("classpath:messages");
          messageSource.setDefaultEncoding("UTF-8");
          return messageSource;
      }
  
      @Bean
      public LocalValidatorFactoryBean getValidator() {
          LocalValidatorFactoryBean bean = new LocalValidatorFactoryBean();
          bean.setValidationMessageSource(messageSource());
          return bean;
      }
  
  }
  </code></pre>
  
  <p>My controller class is:</p>
  
  <pre><code>package com.ashwin.officeproject.controller;
  
  import java.util.ArrayList;
  import java.util.List;
  
  import javax.validation.Valid;
  
  import org.springframework.beans.factory.annotation.Autowired;
  import org.springframework.stereotype.Controller;
  import org.springframework.ui.Model;
  import org.springframework.ui.ModelMap;
  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 com.ashwin.officeproject.model.Office;
  import com.ashwin.officeproject.repository.OfficeRepsitory;
  
  @Controller
  public class MainController {
  
      @Autowired
      OfficeRepsitory officeRepository;
  
  
      @RequestMapping(value = { "/office"}, method = RequestMethod.GET)
      public String office(Model model) {
         model.addAttribute("offices", new Office()); 
        return "office";
      }
  
      @RequestMapping(value = "/addOffice", method = RequestMethod.POST)
      public String submit(@Valid @ModelAttribute("offices") Office office, 
        BindingResult result, ModelMap model) {
          if (result.hasErrors()) {
              return "error";
          }
          officeRepository.save(office);
          model.addAttribute("offices", new Office()); 
          return "office";
      }
  
  }
  </code></pre>
  
  <p>I have a office.jsp page which is:</p>
  
  <pre><code>&lt;%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %&gt;
  &lt;jsp:include page="./header.jsp" /&gt;
   &lt;style&gt;
        .error {
           color: #ff0000;
        }
  
        .errorblock {
           color: #000;
           background-color: #ffEEEE;
           border: 3px solid #ff0000;
           padding: 8px;
           margin: 16px;
        }
     &lt;/style&gt;
      &lt;form:form method="POST"  action="/addOffice"   modelAttribute="offices"  &gt;
  
               &lt;table&gt;
                  &lt;tr&gt;
                      &lt;td&gt;&lt;form:label path="officeName"&gt;Name&lt;/form:label&gt;&lt;/td&gt;
                      &lt;td&gt;&lt;form:input path="officeName"/&gt;&lt;/td&gt;
                      &lt;form:errors path = "officeName" cssClass = "error" /&gt;
  
                  &lt;/tr&gt;
                  &lt;tr&gt;
                      &lt;td&gt;&lt;form:label path="officeNumber"&gt;Number&lt;/form:label&gt;&lt;/td&gt;
                      &lt;td&gt;&lt;form:input path="officeNumber"/&gt;&lt;/td&gt;
  
  
                  &lt;/tr&gt;
                  &lt;tr&gt;
                      &lt;td&gt;&lt;form:label path="officeAddress"&gt;
                        Address&lt;/form:label&gt;&lt;/td&gt;
                      &lt;td&gt;&lt;form:input path="officeAddress"/&gt;&lt;/td&gt;
  
  
                  &lt;/tr&gt;
                  &lt;tr&gt;
                      &lt;td&gt;&lt;input type="submit" value="Submit"/&gt;&lt;/td&gt;
                  &lt;/tr&gt;
              &lt;/table&gt;
          &lt;/form:form&gt;
  
  
  &lt;jsp:include page="./footer.jsp" /&gt;    
  </code></pre>
  
  <p>My pom.xml is:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"&gt;
      &lt;modelVersion&gt;4.0.0&lt;/modelVersion&gt;
      &lt;parent&gt;
          &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
          &lt;artifactId&gt;spring-boot-starter-parent&lt;/artifactId&gt;
          &lt;version&gt;2.1.3.RELEASE&lt;/version&gt;
          &lt;relativePath/&gt; &lt;!-- lookup parent from repository --&gt;
      &lt;/parent&gt;
      &lt;groupId&gt;com.ashwin&lt;/groupId&gt;
      &lt;artifactId&gt;OfficeProject&lt;/artifactId&gt;
      &lt;version&gt;0.0.1-SNAPSHOT&lt;/version&gt;
      &lt;name&gt;OfficeProject&lt;/name&gt;
      &lt;description&gt;OfficeProject&lt;/description&gt;
  
      &lt;properties&gt;
          &lt;java.version&gt;1.8&lt;/java.version&gt;
      &lt;/properties&gt;
  
      &lt;dependencies&gt;
          &lt;dependency&gt;
              &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
              &lt;artifactId&gt;spring-boot-starter-data-jpa&lt;/artifactId&gt;
          &lt;/dependency&gt;
          &lt;dependency&gt;
              &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
              &lt;artifactId&gt;spring-boot-starter-web&lt;/artifactId&gt;
          &lt;/dependency&gt;
  &lt;dependency&gt;
      &lt;groupId&gt;org.hibernate&lt;/groupId&gt;
      &lt;artifactId&gt;hibernate-validator&lt;/artifactId&gt;
      &lt;version&gt;6.0.12.Final&lt;/version&gt;
  &lt;/dependency&gt;
  &lt;dependency&gt;
      &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
      &lt;artifactId&gt;spring-boot-starter-validation&lt;/artifactId&gt;
  &lt;/dependency&gt;
          &lt;dependency&gt;
              &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
              &lt;artifactId&gt;spring-boot-devtools&lt;/artifactId&gt;
              &lt;scope&gt;runtime&lt;/scope&gt;
          &lt;/dependency&gt;
          &lt;dependency&gt;
              &lt;groupId&gt;mysql&lt;/groupId&gt;
              &lt;artifactId&gt;mysql-connector-java&lt;/artifactId&gt;
              &lt;scope&gt;runtime&lt;/scope&gt;
          &lt;/dependency&gt;
          &lt;dependency&gt;
              &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
              &lt;artifactId&gt;spring-boot-starter-test&lt;/artifactId&gt;
              &lt;scope&gt;test&lt;/scope&gt;
          &lt;/dependency&gt;
  
          &lt;dependency&gt;
      &lt;groupId&gt;org.apache.tomcat.embed&lt;/groupId&gt;
      &lt;artifactId&gt;tomcat-embed-jasper&lt;/artifactId&gt;
  &lt;/dependency&gt;
  
  &lt;dependency&gt;
      &lt;groupId&gt;javax.servlet&lt;/groupId&gt;
      &lt;artifactId&gt;jstl&lt;/artifactId&gt;
  &lt;/dependency&gt;
      &lt;/dependencies&gt;
  
      &lt;build&gt;
          &lt;plugins&gt;
              &lt;plugin&gt;
                  &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
                  &lt;artifactId&gt;spring-boot-maven-plugin&lt;/artifactId&gt;
              &lt;/plugin&gt;
          &lt;/plugins&gt;
      &lt;/build&gt;
  
  &lt;/project&gt;
  </code></pre>
  
  <p>When I click the submit button then if the input field in my form are empty,I wanted to validate my form and show the message below the input field like <strong>"Please provide valid office name"</strong> .But what is happening is that when i hit the submit button then it is going to my error.jsp page but it is not showing any validation message which i have declared above.My error page displaying is:</p>
  
  <p>error.jsp</p>
  
  <pre><code>&lt;%@ page language="java" contentType="text/html; charset=ISO-8859-1"
      pageEncoding="ISO-8859-1"%&gt;
  &lt;!DOCTYPE html&gt;
  &lt;html&gt;
  &lt;head&gt;
  &lt;meta charset="ISO-8859-1"&gt;
  &lt;title&gt;Insert title here&lt;/title&gt;
  &lt;/head&gt;
  &lt;body&gt;
  &lt;p&gt;Error&lt;/p&gt;
  &lt;/body&gt;
  &lt;/html&gt;      
  </code></pre>
  
  <p>I am not getting any error in my console in eclipse too.It is just redirecting me in error.jsp page when my form's field is empty.   </p>