title: Spring Validation Exception : BindException
body: <p>on submitting form to <strong>addUser</strong> controller exception occured </p>
  
  <blockquote>
    <p>SEVERE: Servlet.service() for servlet dispatcherServlet threw exception
    org.springframework.validation.BindException: org.springframework.validation.BeanPropertyBindingResult: 1 errors
    Field error in object 'userBean' on field 'email': rejected value [hello]; codes [Email.userBean.email,Email.email,Email.java.lang.String,Email]; arguments [org.springframework.context.support.DefaultMessageSourceResolvable: codes [userBean.email,email]; arguments []; default message [email]]; default message [Not a vaild Email Address]
        at org.springframework.web.method.annotation.ModelAttributeMethodProcessor.resolveArgument(ModelAttributeMethodProcessor.java:111)
        at org.springframework.web.method.support.HandlerMethodArgumentResolverComposite.resolveArgument(HandlerMethodArgumentResolverComposite.java:75)
        at org.springframework.web.method.support.InvocableHandlerMethod.getMethodArgumentValues(InvocableHandlerMethod.java:156)
        at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:117)
        at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:96)
        at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:617)
        at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:578)
        at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:80)
        at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:923)
        at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:852)
        at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:882)
        at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:789)
        at javax.servlet.http.HttpServlet.service(HttpServlet.java:710)
        at javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
        at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
        at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
        at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:230)
        at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:175)
        at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:128)
        at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:104)
        at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
        at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:261)
        at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:844)
        at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:581)
        at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:447)
        at java.lang.Thread.run(Thread.java:744)</p>
  </blockquote>
  
  <p>Controller for getting Form</p>
  
  <pre><code>    @RequestMapping(method = RequestMethod.GET, value = "register")
  public String addUser(Model model) {
      if (!model.containsAttribute("wrongLink")) {
          System.out.println("not wrong Link");
          model.addAttribute(new UserBean());
      } else {
          System.out.println("wrong Link");
      }
      return "user/register";
  }
  </code></pre>
  
  <p>Controller to post form </p>
  
  <pre><code>@RequestMapping(method = RequestMethod.POST, value = "register")
  public String addUser(@Valid UserBean userBean, Model model,
          RedirectAttributes redirectAttrs, BindingResult bindingResult) {
      System.out.println("in addUser form");
      if (bindingResult.hasErrors()) {
          System.out.println("ERROR in user Form");
          return "user/edit";
      }
      return "redirect:/users/" + user.getDisplayName();
  }
  </code></pre>
  
  <p>UserBean class</p>
  
  <pre><code>import org.hibernate.validator.constraints.Email;
  public class UserBean {
  
  private Integer id;
  
  @Email(message = "Not a vaild Email Address")
  private String email;
  //getter and setter
  }
  </code></pre>
  
  <p>Form </p>
  
  <pre><code>&lt;div id="container"&gt;
      &lt;sf:form method="POST" modelAttribute="userBean"&gt;
              &lt;div class="form"&gt;
                  &lt;sf:input path="email" type="text" id="email"
                      placeholder="email address" /&gt;
                  &lt;sf:errors path="email" cssClass="error" /&gt;
                  &lt;input class="send submit" type="submit" name="submit_first"
                      id="submit_first" value="" /&gt;
              &lt;/div&gt;
      &lt;/sf:form&gt;
  &lt;/div&gt;
  </code></pre>
  
  <p>spring.xml</p>
  
  <pre><code>&lt;beans xmlns="http://www.springframework.org/schema/beans"
  xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:context="http://www.springframework.org/schema/context"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="
      http://www.springframework.org/schema/beans     
      http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
      http://www.springframework.org/schema/mvc 
      http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
      http://www.springframework.org/schema/context 
      http://www.springframework.org/schema/context/spring-context-3.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd"
  xmlns:tx="http://www.springframework.org/schema/tx"&gt;
  
  &lt;context:component-scan base-package="com.example" /&gt;
  
  &lt;bean id="mailSender" class="org.springframework.mail.javamail.JavaMailSenderImpl"&gt;
      &lt;property name="host" value="${smtp.host}" /&gt;
      &lt;property name="port" value="${smtp.port}" /&gt;
      &lt;property name="username" value="${smtp.username}" /&gt;
      &lt;property name="password" value="${smtp.password}" /&gt;
      &lt;property name="javaMailProperties"&gt;
          &lt;props&gt;
              &lt;!-- Use SMTP transport protocol --&gt;
              &lt;prop key="mail.transport.protocol"&gt;smtp&lt;/prop&gt;
              &lt;!-- Use SMTP-AUTH to authenticate to SMTP server --&gt;
              &lt;prop key="mail.smtp.auth"&gt;true&lt;/prop&gt;
              &lt;!-- Use TLS to encrypt communication with SMTP server --&gt;
              &lt;prop key="mail.smtp.starttls.enable"&gt;true&lt;/prop&gt;
              &lt;prop key="mail.debug"&gt;true&lt;/prop&gt;
          &lt;/props&gt;
      &lt;/property&gt;
  &lt;/bean&gt;
  
  &lt;bean id="alertMailMessage" class="org.springframework.mail.SimpleMailMessage"&gt;
      &lt;property name="from" value="${alertMailMessage.from}" /&gt;
      &lt;property name="to" value="${alertMailMessage.to}" /&gt;
      &lt;property name="subject" value="${alertMailMessage.subject}" /&gt;
  &lt;/bean&gt;
  
  &lt;bean id="multipartResolver"
      class="org.springframework.web.multipart.commons.CommonsMultipartResolver"&gt;
      &lt;!-- one of the properties available; the maximum file size in bytes --&gt;
      &lt;property name="maxUploadSize" value="1000000" /&gt;
  &lt;/bean&gt;
  
  
  &lt;bean
      class="org.springframework.web.servlet.view.InternalResourceViewResolver"&gt;
      &lt;property name="prefix"&gt;
          &lt;value&gt;/WEB-INF/pages/&lt;/value&gt;
      &lt;/property&gt;
      &lt;property name="suffix"&gt;
          &lt;value&gt;.jsp&lt;/value&gt;
      &lt;/property&gt;
  &lt;/bean&gt;
  
  &lt;mvc:resources mapping="/**" location="/resources/" /&gt;
  
  &lt;mvc:annotation-driven /&gt;
  
  &lt;bean id="propertyConfigurer"
      class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"&gt;
      &lt;property name="locations"&gt;
          &lt;list&gt;
              &lt;value&gt;/WEB-INF/properties/database.properties&lt;/value&gt;
              &lt;value&gt;/WEB-INF/properties/smtp.properties&lt;/value&gt;
          &lt;/list&gt;
      &lt;/property&gt;
  &lt;/bean&gt;
  
  &lt;bean id="dataSource"
      class="org.springframework.jdbc.datasource.DriverManagerDataSource"&gt;
      &lt;property name="driverClassName" value="${jdbc.driver}" /&gt;
      &lt;property name="url" value="${jdbc.url}" /&gt;
      &lt;property name="username" value="${jdbc.user}" /&gt;
      &lt;property name="password" value="${jdbc.password}" /&gt;
  &lt;/bean&gt;
  
  &lt;bean id="sessionFactory"
      class="org.springframework.orm.hibernate4.LocalSessionFactoryBean"&gt;
      &lt;property name="dataSource" ref="dataSource" /&gt;
      &lt;property name="packagesToScan" value="com.example.model" /&gt;
      &lt;property name="hibernateProperties"&gt;
          &lt;props&gt;
              &lt;prop key="hibernate.dialect"&gt;${hibernate.dialect}&lt;/prop&gt;
              &lt;prop key="hibernate.show_sql"&gt;${hibernate.show_sql}&lt;/prop&gt;
              &lt;prop key="hibernate.hbm2ddl.auto"&gt;${hibernate.hbm2ddl.auto}&lt;/prop&gt;
          &lt;/props&gt;
  
      &lt;/property&gt;
  &lt;/bean&gt;
  
  
  &lt;tx:annotation-driven transaction-manager="transactionManager" /&gt;
  
  &lt;bean id="transactionManager"
      class="org.springframework.orm.hibernate4.HibernateTransactionManager"&gt;
      &lt;property name="sessionFactory" ref="sessionFactory" /&gt;
  &lt;/bean&gt;
  </code></pre>
  
  <p></p>
  
  <p>why this exception is coming on entering invalid email rather than validation has to be performed.</p>