title: Type mismatch when I enter string for Int in one of the form parameter when I submit form in spring 3
body: <p>I have a jsp form as following :</p>
  
  <pre><code>&lt;form:form method="POST" commandName="command" action="RouteGradesGoals.html" &gt;
  &lt;tr&gt;
     &lt;td class="style4" colspan="2"&gt;
      Goal&lt;/td&gt;
      &lt;td colspan="2"&gt;
          &lt;form:input path="goal" type="text" id="goal" style="width:200px;" /&gt;
       &lt;/td&gt;
       &lt;td colspan="2"&gt;
           &lt;form:errors path="goal" cssClass="error" /&gt;
       &lt;/td&gt;
   &lt;/tr&gt;
   &lt;input type="submit" name="action" value="Delete" id="Button3" 
                          class="submitButton" /&gt;
  &lt;/form:form&gt;
  </code></pre>
  
  <p>As you see there is only one parameter goal in the command object and it is of type int in the command object.</p>
  
  <p>But when I submit the form I gets this error by passing string for int in the goal param:</p>
  
  <p>{"detail":"An error has occured while processing the request: org.springframework.validation.BeanPropertyBindingResult: 1 errors\nField error in object 'command' on field 'goal': rejected value [sadfasd]; codes [typeMismatch.command.goal,typeMismatch.goal,typeMismatch.java.lang.Integer,typeMismatch]; arguments [org.springframework.context.support.DefaultMessageSourceResolvable: codes [command.goal,goal]; arguments []; default message [goal]]; default message [Failed to convert property value of type 'java.lang.String' to required type 'java.lang.Integer' for property 'goal'; nested exception is java.lang.NumberFormatException: For input string: \"sadfasd\"]"}</p>
  
  <p>The controller has a simple method to process this form as given below:</p>
  
  <pre><code>@RequestMapping( value = "/RouteGradesGoals.html", method = RequestMethod.POST )
      public String addInventoryGoal(@ModelAttribute( "command" ) InventoryGoal goal, HttpServletRequest request,
              BindingResult result, SessionStatus status, ModelMap model, HttpSession session){
          inventoryGoalValidator.validate(goal, result);
          if (result.hasErrors()) {
              return "RouteGradesGoals";
          }
  
          // do processing of the command object 
          return "RouteGradesGoals";
  
      }
  </code></pre>
  
  <p>Command object is as follows :</p>
  
  <pre><code>public class InventoryGoal {
  
  private Integer goal;
  
  public Integer getGoal() {
          return goal;
      }
  
      public void setGoal(Integer goal) {
          this.goal = goal;
      }
  
  }
  </code></pre>
  
  <p>This is how I have added the message resolver in the context xml file :</p>
  
  <pre><code>&lt;bean id="messageSource"
      class="org.springframework.context.support.ReloadableResourceBundleMessageSource"&gt;
          &lt;property name="basename" value="classpath:messages" /&gt;
          &lt;property name="defaultEncoding" value="UTF-8"/&gt;
      &lt;/bean&gt;
  
  &lt;bean id="localeChangeInterceptor"
      class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor"&gt;
      &lt;property name="paramName" value="lang" /&gt;
  &lt;/bean&gt;
  
  &lt;bean id="localeResolver"
      class="org.springframework.web.servlet.i18n.CookieLocaleResolver"&gt;
      &lt;property name="defaultLocale" value="en"/&gt;
  &lt;/bean&gt;
  
  &lt;bean id="handlerMapping"
      class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping"&gt;
      &lt;property name="interceptors"&gt;
          &lt;ref bean="localeChangeInterceptor" /&gt;
      &lt;/property&gt;
  &lt;/bean&gt;
  </code></pre>
  
  <p>I have added these properties in the messages.properties</p>
  
  <pre><code>typeMismatch.command.goal=invalid goal1
  typeMismatch.goal=invalid goal2
  typeMismatch.java.lang.Integer=invalid goal2
  typeMismatch=invalid goal3
  </code></pre>
  
  <p>web.xml :</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
      xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
      version="2.5"&gt;
  
      &lt;display-name&gt;Forerunner Webservice&lt;/display-name&gt;
      &lt;context-param&gt;
          &lt;param-name&gt;contextClass&lt;/param-name&gt;
          &lt;param-value&gt;org.springframework.web.context.support.AnnotationConfigWebApplicationContext&lt;/param-value&gt;
      &lt;/context-param&gt;
      &lt;context-param&gt;
          &lt;param-name&gt;contextConfigLocation&lt;/param-name&gt;
          &lt;param-value&gt;com.ids.forerunner.web.configuration.WebConfig&lt;/param-value&gt;
      &lt;/context-param&gt;
      &lt;context-param&gt;
          &lt;param-name&gt;spring.profiles.active&lt;/param-name&gt;
          &lt;param-value&gt;mysql&lt;/param-value&gt;
      &lt;/context-param&gt;
      &lt;servlet&gt;
          &lt;servlet-name&gt;restService&lt;/servlet-name&gt;
          &lt;servlet-class&gt;org.springframework.web.servlet.DispatcherServlet&lt;/servlet-class&gt;
          &lt;init-param&gt;
              &lt;param-name&gt;contextConfigLocation&lt;/param-name&gt;
              &lt;param-value&gt;&lt;/param-value&gt;
          &lt;/init-param&gt;
          &lt;load-on-startup&gt;2&lt;/load-on-startup&gt;
      &lt;/servlet&gt;
      &lt;servlet&gt;
          &lt;servlet-name&gt;webapp&lt;/servlet-name&gt;
          &lt;servlet-class&gt;org.springframework.web.servlet.DispatcherServlet&lt;/servlet-class&gt;
          &lt;load-on-startup&gt;1&lt;/load-on-startup&gt;
      &lt;/servlet&gt;
      &lt;servlet-mapping&gt;
          &lt;servlet-name&gt;webapp&lt;/servlet-name&gt;
          &lt;url-pattern&gt;/webapp/*&lt;/url-pattern&gt;
      &lt;/servlet-mapping&gt;
      &lt;servlet-mapping&gt;
          &lt;servlet-name&gt;restService&lt;/servlet-name&gt;
          &lt;url-pattern&gt;/&lt;/url-pattern&gt;
      &lt;/servlet-mapping&gt;
      &lt;filter&gt;
          &lt;!-- The CORS filter with parameters --&gt;
          &lt;filter-name&gt;CORS&lt;/filter-name&gt;
          &lt;filter-class&gt;com.ids.cors.CORSFilter&lt;/filter-class&gt;
  
          &lt;!-- Note: All parameters are options, if ommitted CORS Filter
               will fall back to the respective default values.
            --&gt;
          &lt;init-param&gt;
              &lt;param-name&gt;cors.allowGenericHttpRequests&lt;/param-name&gt;
              &lt;param-value&gt;true&lt;/param-value&gt;
          &lt;/init-param&gt;
  
          &lt;init-param&gt;
              &lt;param-name&gt;cors.allowOrigin&lt;/param-name&gt;
              &lt;param-value&gt;*&lt;/param-value&gt;
          &lt;/init-param&gt;
  
          &lt;init-param&gt;
              &lt;param-name&gt;cors.supportedMethods&lt;/param-name&gt;
              &lt;param-value&gt;GET, HEAD, POST, OPTIONS&lt;/param-value&gt;
          &lt;/init-param&gt;
  
          &lt;init-param&gt;
              &lt;param-name&gt;cors.supportedHeaders&lt;/param-name&gt;
              &lt;param-value&gt;Content-Type, X-Requested-With&lt;/param-value&gt;
          &lt;/init-param&gt;
  
          &lt;init-param&gt;
              &lt;param-name&gt;cors.exposedHeaders&lt;/param-name&gt;
              &lt;param-value&gt;X-Test-1, X-Test-2&lt;/param-value&gt;
          &lt;/init-param&gt;
  
          &lt;init-param&gt;
              &lt;param-name&gt;cors.supportsCredentials&lt;/param-name&gt;
              &lt;param-value&gt;true&lt;/param-value&gt;
          &lt;/init-param&gt;
  
          &lt;init-param&gt;
              &lt;param-name&gt;cors.maxAge&lt;/param-name&gt;
              &lt;param-value&gt;3600&lt;/param-value&gt;
          &lt;/init-param&gt;
  
      &lt;/filter&gt;
  
  
      &lt;filter-mapping&gt;
          &lt;!-- CORS Filter mapping --&gt;
          &lt;filter-name&gt;CORS&lt;/filter-name&gt;
          &lt;url-pattern&gt;/*&lt;/url-pattern&gt;
      &lt;/filter-mapping&gt;
  
      &lt;filter&gt;
          &lt;filter-name&gt;APIProtectionFilter&lt;/filter-name&gt;
          &lt;filter-class&gt;com.ids.forerunner.filter.APIProtectionFilter&lt;/filter-class&gt;
      &lt;/filter&gt;
      &lt;filter-mapping&gt;
          &lt;filter-name&gt;APIProtectionFilter&lt;/filter-name&gt;
          &lt;url-pattern&gt;/*&lt;/url-pattern&gt;
      &lt;/filter-mapping&gt;
      &lt;welcome-file-list&gt;
          &lt;welcome-file&gt;index.html&lt;/welcome-file&gt;
      &lt;/welcome-file-list&gt;
      &lt;listener&gt;
          &lt;listener-class&gt;org.springframework.web.context.ContextLoaderListener&lt;/listener-class&gt;
      &lt;/listener&gt;
  &lt;/web-app&gt;
  </code></pre>
  
  <p>in the WebConfig.java I am importing resources as given below:</p>
  
  <p>@ImportResource({"classpath:/META-INF/spring/forerunner-service.xml","/WEB-INF/webapp-servlet.xml"})</p>
  
  <p>webapp-servlet.xml :</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;beans xmlns="http://www.springframework.org/schema/beans"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
      xmlns:context="http://www.springframework.org/schema/context"
      xmlns:mvc="http://www.springframework.org/schema/mvc"
      xmlns:util="http://www.springframework.org/schema/util"
      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.1.xsd
  http://www.springframework.org/schema/context
  http://www.springframework.org/schema/context/spring-context-3.0.xsd
  http://www.springframework.org/schema/util 
  http://www.springframework.org/schema/util/spring-util-3.0.xsd"&gt;
  
      &lt;bean id="viewResolver"
          class="org.springframework.web.servlet.view.InternalResourceViewResolver"&gt;
          &lt;property name="prefix"&gt;
              &lt;value&gt;/WEB-INF/jsp/&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;context:component-scan base-package="com.ids.forerunner.webapp.controllers" /&gt;
  
      &lt;mvc:resources mapping="/css/**" location="/css/" /&gt;
      &lt;mvc:resources mapping="/images/**" location="/images/" /&gt;
      &lt;mvc:resources mapping="/js/**" location="/js/" /&gt;
      &lt;mvc:resources mapping="/forerunner_style.css" location="/forerunner_style.css" /&gt;
  
      &lt;mvc:interceptors&gt;
          &lt;mvc:interceptor&gt;
              &lt;mvc:mapping path="/**" /&gt;
              &lt;bean class="com.ids.forerunner.webapp.interceptor.ForerunnerRequestInterceptor" /&gt;
          &lt;/mvc:interceptor&gt;
      &lt;/mvc:interceptors&gt;
  
      &lt;bean id="messageSource" class=" org.springframework.context.support.ReloadableResourceBundleMessageSource"&gt;
      &lt;property name="basename" value="classpath:message-webapp-error" /&gt;
  &lt;/bean&gt;
  
  &lt;!--    &lt;util:properties id="tophireProperties" location="classpath:tophire.properties" /&gt; --&gt; 
  
  &lt;/beans&gt;
  </code></pre>
  
  <p>forerunner-service.xml:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;beans xmlns="http://www.springframework.org/schema/beans"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"
      xmlns:c="http://www.springframework.org/schema/c" xmlns:cache="http://www.springframework.org/schema/cache"
      xmlns:context="http://www.springframework.org/schema/context"
      xmlns:jee="http://www.springframework.org/schema/jee" xmlns:lang="http://www.springframework.org/schema/lang"
      xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:p="http://www.springframework.org/schema/p"
      xmlns:task="http://www.springframework.org/schema/task" xmlns:util="http://www.springframework.org/schema/util"
      xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
          http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.1.xsd
          http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache-3.1.xsd
          http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd
          http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.1.xsd
          http://www.springframework.org/schema/lang http://www.springframework.org/schema/lang/spring-lang-3.1.xsd
          http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd
          http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-3.1.xsd
          http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd"&gt;
  
      &lt;!-- Enables automatic mapping of fund objects to and from JSON --&gt;
      &lt;mvc:annotation-driven /&gt;
  
      &lt;import resource="classpath:/META-INF/spring/applicationContext.xml" /&gt;
      &lt;import resource="classpath:/META-INF/spring/applicationContext-jpa.xml" /&gt;
  
      &lt;bean id="contactFormValidator" class="com.ids.forerunner.webapp.validator.ContactFormValidator" /&gt;
      &lt;bean id="inventoryGoalValidator" class="com.ids.forerunner.webapp.validator.InventoryGoalValidator" /&gt;
      &lt;bean id="anchorAreaValidator" class="com.ids.forerunner.webapp.validator.AnchorAreaValidator" /&gt;
  
      &lt;bean class="com.ids.forerunner.util.ForerunnerInitializer"/&gt;
      &lt;bean class="org.dozer.spring.DozerBeanMapperFactoryBean"&gt;
          &lt;property name="mappingFiles" value="classpath*:/dozer-bean-mappings.xml" /&gt;
      &lt;/bean&gt;
  
      &lt;!-- Setup spring to pull in @Controller, @RequestMapping, etc. Configuration 
          scans specified packages for classes configured as Spring managed beans and 
          automatically sets up objects annotated with @Controller, @Service etc. --&gt;
      &lt;context:component-scan base-package="com.ids.forerunner.web" /&gt;
  
      &lt;bean
          class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping"&gt;&lt;/bean&gt;
  
      &lt;!-- Configures view for returning JSON to the client --&gt;
      &lt;bean
          class="org.springframework.web.servlet.view.json.MappingJacksonJsonView"
          p:contentType="text/plain"&gt;&lt;/bean&gt;
  
      &lt;!-- maps handler methods based on HTTP paths --&gt;
      &lt;bean
          class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"&gt;
          &lt;property name="messageConverters"&gt;
              &lt;util:list id="beanList"&gt;
                  &lt;ref bean="jsonMessageConverter" /&gt;
              &lt;/util:list&gt;
          &lt;/property&gt;
      &lt;/bean&gt;
      &lt;!-- Converts JSON to POJO and vice versa --&gt;
      &lt;bean id="jsonMessageConverter"
          class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter" /&gt;
  
      &lt;bean id="viewResolver"
          class="org.springframework.web.servlet.view.InternalResourceViewResolver"
          p:prefix="/WEB-INF/pages/" p:suffix=".jsp"&gt;
      &lt;/bean&gt;
  
      &lt;bean id="messageSource"
      class="org.springframework.context.support.ReloadableResourceBundleMessageSource"&gt;
          &lt;property name="basename" value="classpath:messages" /&gt;
          &lt;property name="defaultEncoding" value="UTF-8"/&gt;
      &lt;/bean&gt;
  
  &lt;bean id="localeChangeInterceptor"
      class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor"&gt;
      &lt;property name="paramName" value="lang" /&gt;
  &lt;/bean&gt;
  
  &lt;bean id="localeResolver"
      class="org.springframework.web.servlet.i18n.CookieLocaleResolver"&gt;
      &lt;property name="defaultLocale" value="en"/&gt;
  &lt;/bean&gt;
  
  &lt;bean id="handlerMapping"
      class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping"&gt;
      &lt;property name="interceptors"&gt;
          &lt;ref bean="localeChangeInterceptor" /&gt;
      &lt;/property&gt;
  &lt;/bean&gt;
  
  &lt;bean id="taskList" class="com.forerunner.cronjobs.TaskList"&gt;
          &lt;property name="taskList"&gt;
              &lt;list&gt;
                  &lt;bean name="sessionTask" class="com.forerunner.cronjobs.SessionCronTask"&gt;&lt;/bean&gt;
              &lt;/list&gt;
          &lt;/property&gt;
      &lt;/bean&gt;
      &lt;bean name="jobDetail" class="org.springframework.scheduling.quartz.JobDetailBean"&gt;
          &lt;property name="jobClass" value="com.forerunner.cronjobs.ForerunnerCronJob" /&gt;
          &lt;property name="jobDataAsMap"&gt;
              &lt;map&gt;
                  &lt;entry key="taskList" value-ref="taskList" /&gt;
              &lt;/map&gt;
          &lt;/property&gt;
      &lt;/bean&gt;
  
      &lt;bean id="cronTrigger" class="com.forerunner.cronjobs.ForerunnerTrigger"&gt;
          &lt;property name="jobDetail" ref="jobDetail" /&gt;
      &lt;/bean&gt;
      &lt;bean class="org.springframework.scheduling.quartz.SchedulerFactoryBean"&gt;
          &lt;property name="jobDetails"&gt;
              &lt;list&gt;
                  &lt;ref bean="jobDetail" /&gt;
              &lt;/list&gt;
          &lt;/property&gt;
  
          &lt;property name="triggers"&gt;
              &lt;list&gt;
                  &lt;ref bean="cronTrigger" /&gt;
              &lt;/list&gt;
          &lt;/property&gt;
      &lt;/bean&gt;
  &lt;/beans&gt;
  </code></pre>
  
  <p>In the message-webapp-error.properties I have properties as follows :</p>
  
  <pre><code>typeMismatch.command.goal=invalid goal1
  typeMismatch.goal=invalid goal2
  typeMismatch.java.lang.Integer=invalid goal2
  typeMismatch=invalid goal3
  </code></pre>
  
  <p>Form is posted to this url http:///forerunner/webapp/RouteGradesGoals.html</p>
  
  <p>Can someone please tell me how can I prevent such error and in such scenerios return a valid error message.</p>