2

I am using auto-binding feature for field skills (Array list) in my View:

...
        <p>
            Student's Skills <select name="skills" multiple>
                <option value="Java Core"> Java Core </option>
                <option value="Spring Core"> Spring Core </option>
                <option value="Spring MVC"> Spring MVC </option>
            </select>
        </p>
(Action is for ` "/MySpringMVCProject3/submitAddmission.html" method="post" `)
...

And this is my model class:

public class Student {
...//name, age fields
private ArrayList<String> skills;

public ArrayList<String> getSkills() {
  return skills;
}

public void setSkills(ArrayList<String> skils) {
    this.skills = skils;
}

//other getter/setters 

}

This is my controller:

@Controller
public class AdmissionController {

@RequestMapping(value = "/submitAddmission.html", method = RequestMethod.POST)
public ModelAndView submitAdmissionForm(@ModelAttribute("st1") Student student1, BindingResult result) {
    if (result.hasErrors()) {
        ModelAndView model = new ModelAndView("AdmissionForm");
        return model;
    }
    ModelAndView model2 = new ModelAndView("AdmissionSuccess");
    return model2;
   }
}

But when i clicked to submit button, this binding result error appears:

Failed to convert property value of type java.lang.String[] to required type java.util.ArrayList for property skills; nested exception is java.lang.IllegalStateException: Cannot convert value of type [java.lang.String[]] to required type [java.util.ArrayList] for property skills: no matching editors or conversion strategy found

Why Spring expected an array of String instead of String arraylist while skills type is an String arraylist?

  • 1
    Use `List` instead of `ArrayList` you should be programming to interfaces and not concrete classes. – M. Deinum Feb 02 '15 at 15:20
  • can you also post your servlet configuration, it seems that some of the default converters are not registered, did you configure a custom conversion service – Master Slave Feb 02 '15 at 16:37

2 Answers2

1

When you post a form with a multiple select option, Spring parses the parameters in an array of Strings.

Let's take a closer look at your error message.

Line 1:

Failed to convert property value of type java.lang.String[] to required type java.util.ArrayList for property skills;

Spring parses the String[] from the URL parameters and doing:

String[] input = { "foo", "bar" };
ArrayList<String> skills = (ArrayList<String>) input;

is obviously going to fail, since Java doesn't automatically know how to typecast it. However, there are a few simple conversions built in, like String[] into List<String>, as shown here.

Line 2:

nested exception is java.lang.IllegalStateException: Cannot convert value of type [java.lang.String[]] to required type [java.util.ArrayList] for property skills: no matching editors or conversion strategy found

You can teach Spring to convert basically anything into anything, if you define a proper conversion strategy. This works by building a Converter class to automatically convert A into B and then teaching Spring to use it. Here's another answer, that outlines how to do that.

Community
  • 1
  • 1
t0mppa
  • 3,983
  • 5
  • 37
  • 48
  • all true, but the thing is that since 3.0 spring MVC ships the ArrayToCollectionConverter which handles the OP's case. The confusing bit is why that converter did not kick in – Master Slave Feb 02 '15 at 16:33
  • Can you post a link to some documentation describing how said class works? All I can find is a [ArrayToCollection](http://docs.spring.io/spring-webflow/docs/2.4.0.RELEASE/api/org/springframework/binding/convert/converters/ArrayToCollection.html), but that's in Spring Web Flow. – t0mppa Feb 02 '15 at 17:29
  • I was speaking from the top of my head, so missspelled, its [ArrayToCollectionConverter](https://github.com/spring-projects/spring-framework/blob/master/spring-core/src/main/java/org/springframework/core/convert/support/ArrayToCollectionConverter.java) your post fixed the issue, so vote-up its what counts, but I would expect that this conversion is built-in – Master Slave Feb 02 '15 at 21:59
-1

add mvc:annotation-driven namespace in xxxx-dispatcher-servlet.xml

Kokozaurus
  • 639
  • 7
  • 22
  • 1
    Can you add an example of the addition to `xxxx-dispatcher-servlet.xml` that the OP should do, in order to fix the problem? – Kokozaurus Oct 05 '16 at 13:11