如何在Spring中将列表用作模型属性? [英] How to use a List as a Model Attribute in Spring?

查看:42
本文介绍了如何在Spring中将列表用作模型属性?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这样的Home控制器:

I have my Home controller like this:

    @RequestMapping("/")
    public ModelAndView welcome(@ModelAttribute("myValuesInRows") List<String> myValuesInRows,  ModelMap model) {
        List<Spravochnik> dropDown = spravochnikService.findAll("sprav_of_spravs");
        List<String> justValuesInRows = new ArrayList<>();
        for(Spravochnik sprav : dropDown) {
            for(List<String> vals : sprav.getValuesInRows()) {
                for(String v : vals) {
                    justValuesInRows.add(v);
                }
            }
        }
        for(int i=1; i<justValuesInRows.size(); i+=2) {
            myValuesInRows.add(justValuesInRows.get(i));
        }
        model.addAttribute("myValuesInRows", myValuesInRows);
        return new ModelAndView("home", model);
    }

并且我的主视图具有我正在使用的选择":

and my Home view has this Select I'm using:

<f:form>
<f:select path="myValuesInRows" items="${myValuesInRows}" name="tableName" id="tableName">
</f:select>
</f:form>

当我尝试显示它时,Spring显示此错误:

When I try to show it Spring shows this error:

找不到接口java.util.List 的主要或默认构造函数.

No primary or default constructor found for interface java.util.List.

我想将我的选择链接到我正在传递的列表中,如何在这里完成?

I would like to connect my select to this List I'm passing, how is it done here?

推荐答案

使用实现 List 的类之一,例如

Use one of the classes which implement List, such as ArrayList or LinkedList, which have default constructors.

@ModelAttribute("myValuesInRows") ArrayList<String> myValuesInRows

您可以在此处找到 List 的所有已知实现类的列表:

You can find a list of all known implementing classes of List here:

https://docs.oracle.com/javase/8/docs/api/java/util/List.html

这篇关于如何在Spring中将列表用作模型属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆