java_dude
java_dude

Reputation: 4088

How to populate Form from Spring dropdown when form is submitted

My code lists the data that I would like to see, but when I hit submit it fails to populate the backing form with exception below. How can I make the bindings work? The exception I get is of Mismatch type, trying to insert a String in List when expecting Objects. Which makes sense.

Example

<tr>
<td><form:select id="myTypes" path="myTypes" multiple="false">
       <form:option value="NONE" label="--- Select ---" />
       <form:options items="${form.myTypes}" itemValue="id" itemLabel="label"/>
     </form:select>
</td>
<td><form:errors path="myTypes" cssClass="error" /></td>

This is how form looks like

public class MyForm {
   List<MyType> myTypes;

   public List<MyType> getMyTypes() {
      return myTypes;
   } 

   public void setMyTypes(List<MyType> myTypes) {
      this.myTypes = myTypes;
   }      
}

And of course MyType has id and label.

Link to above sample code and exception below

HTTP Status 500 - Request processing failed; nested exception is org.springframework.validation.BindException: org.springframework.validation.BeanPropertyBindingResult: 1 errors

Field error in object 'Form' on field 'myTypes': rejected value [8768743658734587345]; codes [typeMismatch.Form.myTypes,typeMismatch.myTypes,typeMismatch.java.util.List,typeMismatch]; arguments [org.springframework.context.support.DefaultMessageSourceResolvable: codes [myForm.myTypes,myTypes]; arguments []; default message [myTypes]]; default message [Failed to convert property value of type 'java.lang.String' to required type 'java.util.List' for property 'myTypes'; nested exception is java.lang.IllegalStateException: Cannot convert value of type [java.lang.String] to required type [com.x.x.MyTypeEntity] for property 'myTypes[0]': no matching editors or conversion strategy found]

Solution:

Make sure you are mapping to Single element and not to the list :) It should be something like

 <form:select path="someEntity[${status.index}].myType.id">

HTH

Upvotes: 0

Views: 831

Answers (1)

Comencau
Comencau

Reputation: 1193

I think the problem is that Spring does not know how to convert the selected option value (which is a String posted towards your app as an HTTP parameter named "myTypes" when you submit the form) to a MyType object. You should configure a Formatter< MyType > and register it to the Spring FormatterRegistry (see Spring doc) to let Spring know how to convert the incoming String to a MyType object.

    public class MyTypeFormatter implements Formatter<MyType> {
    @Override
    public MyType parse(String text, Locale locale) throws ParseException {
        return myTypeService.getType(text); // for example
    }

    public String print(MyType t, Locale locale) {
        return t.getId();// for example
    };
}

By the way, if I may, since your dropdown list is not multiple, it means that you are going to select just one of the available MyType options. The path of the < form:select > should be named "myType" instead of "myTypes" and especially, it should refer to a MyType attribute within your Form object and not to a List< MyType > attribute. Maybe you should name your first list of available MyType objects "availableTypes" and create a second attribute named "selectedType" to bind the MyType object corresponding to the selected option on the GUI.

Upvotes: 1

Related Questions