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

來源:互聯網
上載者:User

標籤:val   [1]   技術   click   ide   傳參   過程   sed   頁面   

最近項目中頁面比較複雜,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內封裝的不是基礎資料型別 (Elementary Data Type),而是一個對象,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

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.