简体   繁体   中英

Spring mvc arraylist auto binding converts into string array

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 ?

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.

xxxx-dispatcher-servlet.xml添加mvc:annotation-driven命名空间

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM