首页 > 代码库 > Could not instantiate bean class [java.util.List]: Specified class is an interface] with root cause

Could not instantiate bean class [java.util.List]: Specified class is an interface] with root cause

最近项目中页面比较复杂,springMVC传参过程中遇到这样一个错误:Could not instantiate bean class [java.util.List]: Specified class is an interface] with root cause

经研究发现这是参数封装出了问题。

还原代码:

@RequestMapping("/test")    public ModelAndView test(List<OptionVo> ov){        ModelAndView view = new ModelAndView("list");        return view;    }

VO对象:

public class OptionVo {	private String name;	private String options;	private List<String> option;	public String getName() {		return name;	}	public void setName(String name) {		this.name = name;	}	public String getOptions() {		return options;	}	public void setOptions(String options) {		this.options = options;	}	public List<String> getOption() {		return option;	}	public void setOption(List<String> option) {		this.option = option;	}}

  可以看出,controller中参数List内封装的不是基本数据类型,而是一个对象,springMVC源码获取前台的参数是:request.getParameter("")来接收参数的,这样的话,封装参数时就出问题了。

解决办法:

将VO对象再进行封装:

public class Form {    private List<OptionVo> o;    public List<OptionVo> getO() {        return o;    }    public void setO(List<OptionVo> o) {        this.o = o;    }}

controller:

@RequestMapping("/test")    public ModelAndView test(Form formm){        ModelAndView view = new ModelAndView("list");        return view;    }

前台页面:

技术分享
<input type="text" name="o[0].name" value="爱好">        <input type="text" name="o[0].options" value="爱好">        <input type="hidden" name="o[0].option" value="1">        <input type="hidden" name="o[0].option" value="2">        <input type="hidden" name="o[0].option" value="3">        <input type="hidden" name="o[0].option" value="4">        <hr>        <input type="text" name="o[1].name" value="哈哈">        <input type="text" name="o[1].options" value="爱好2">        <input type="hidden" name="o[1].option" value="1">        <input type="hidden" name="o[1].option" value="2">        <input type="hidden" name="o[1].option" value="3">        <input type="hidden" name="o[1].option" value="4">        <hr>
View Code

 

Could not instantiate bean class [java.util.List]: Specified class is an interface] with root cause