title: JSP : How to link form select option to Java object
body: <p>I have two objects which are linked. <code>Address</code> which has a <code>Country</code> with a <code>ManyToOne</code> relationship linked by <code>countryId</code>. I am trying to add a webpage using JSP to add a new <code>Address</code>. On this page you are able to select a <code>Country</code> from a dropdown. </p>
  
  <p>I have tried a number of different ways to do this but i cannot seem to link the <code>Country</code> object in the dropdown to the <code>Country</code> object in the <code>Address</code>, i keep getting the following error</p>
  
  <pre><code>Field error in object 'address' on field 'country': rejected value [com.company.project.domain.Country@670b11d7]; codes [typeMismatch.address.country,typeMismatch.country,typeMismatch.com.company.project.domain.Country,typeMismatch]; arguments [org.springframework.context.support.DefaultMessageSourceResolvable: codes [address.country,country]; arguments []; default message [country]]; default message [Failed to convert property value of type 'java.lang.String' to required type 'com.company.project.domain.Country' for property 'country'; nested exception is java.lang.IllegalStateException: Cannot convert value of type [java.lang.String] to required type [com.company.project.domain.Country] for property 'country': no matching editors or conversion strategy found
  </code></pre>
  
  <p>These are two of many different options i have tried but i keep getting the same error</p>
  
  <p><strong><em>Try 1</em></strong>
  <strong>JAVA</strong></p>
  
  <pre><code>List&lt;Country&gt; countries = countryService.getAllCountrys();
      LinkedHashMap&lt;Country, String&gt; countryMap = new LinkedHashMap&lt;Country, String&gt;();
      for(Country country : countries){
          countryMap.put(country, country.getCountryName());
      }
  
      model.addAttribute("countries", countryMap);
  </code></pre>
  
  <p><strong><em>Try 1</em></strong>
  <strong>JSP</strong></p>
  
  <pre><code>&lt;tr&gt;
      &lt;td&gt;Country:&lt;/td&gt;
      &lt;td&gt;&lt;form:select path="country"&gt;
      &lt;form:options items="${countries}" /&gt;
      &lt;/form:select&gt;
      &lt;/td&gt;
  &lt;/tr&gt;
  </code></pre>
  
  <p><strong><em>Try 2</em></strong>
  <strong>JAVA</strong></p>
  
  <pre><code>List&lt;Country&gt; countries= countryService.getAllCountry();
      model.addAttribute("countries", countries);
  </code></pre>
  
  <p><strong><em>Try 2</em></strong>
  <strong>JSP</strong></p>
  
  <pre><code>&lt;tr&gt;
      &lt;td&gt;Country:&lt;/td&gt;
      &lt;td&gt;&lt;form:select path="country" id="country"&gt;
          &lt;form:options items="${countries}" itemValue="country" itemLabel="countryName" /&gt;
      &lt;/form:select&gt;
  &lt;/tr&gt;
  </code></pre>
  
  <p>I can get the dropdowns populating correctly but wen i try to save it is seems to be trying to save a string value rather then the object value. I am using a POST request to save the page</p>
  
  <p>Does anyone know how  to do this?</p>