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?

2
  • 1
    Use List instead of ArrayList you should be programming to interfaces and not concrete classes. Commented Feb 2, 2015 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 Commented Feb 2, 2015 at 16:37

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

3 Comments

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
Can you post a link to some documentation describing how said class works? All I can find is a ArrayToCollection, but that's in Spring Web Flow.
I was speaking from the top of my head, so missspelled, its ArrayToCollectionConverter your post fixed the issue, so vote-up its what counts, but I would expect that this conversion is built-in
-1

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

1 Comment

Can you add an example of the addition to xxxx-dispatcher-servlet.xml that the OP should do, in order to fix the problem?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.