简体   繁体   English

通过表单提交创建ArrayList(Thymeleaf + Spring MVC)

[英]Creating ArrayList through Form submitting ( Thymeleaf + Spring MVC )

i wanted to know, how Lists are getting binded in Spring...and how to access them in Thymeleaf. 我想知道,列表是如何在春季绑定的……以及如何在Thymeleaf中访问它们。 With and without WrapperClass How could achieve adding single / multiple objects through an form to an ArrayList or normal Array.. 使用和不使用WrapperClass如何实现通过表单将单个/多个对象添加到ArrayList或普通Array。

I would be very thankful for an explanation. 我非常感谢您的解释。

Thats what i have tried: Form 那就是我尝试过的:表格

<form action="#" th:action="@{/tests}" th:object="${features}" method="post">



        <input type="text" th:field="*{featureArrayList[0].name}"/>
    <input type="text" th:field="*{featureArrayList[1].name}"/>




    <p><input type="submit" value="Submit" /> <input type="reset" value="Reset" /></p>
</form>

Wrapper Class 包装类

  ArrayList<Feature> featureArrayList = new ArrayList<Feature>();

    public FeatureWrapper() {

    }

    public ArrayList<Feature> getFeatureArrayList() {
        return featureArrayList;
    }

    public void setFeatureArrayList(ArrayList<Feature> featureArrayList) {
        this.featureArrayList = featureArrayList;
    }

Model: 模型:

   private String name;

    public Feature(String name) {
        this.name = name;
    }

    public Feature() {
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

Controller 调节器


      @GetMapping("/tests")
    public String showForm(Model model)
    {
        FeatureWrapper featureWrapper = new FeatureWrapper();

        model.addAttribute("features",  featureWrapper);
        return "Feature";
    }
    @PostMapping("/tests")
    public String proceedForm(@ModelAttribute("features") FeatureWrapper features,Model model)
    {
       for(Feature feature: features.getFeatureArrayList())
       {
           System.out.println(feature.getName());
           System.out.println(features.getFeatureArrayList().size());
       }

        return "Feature";
    }

Edit1: Now its working with an Wrapper Class and using static Indexes... How would i achieve this without a wrapper class , i read much about the EL expression ${var.index} , but how is this working ? Edit1:现在,它可以与包装类一起使用并使用静态索引...如果没有包装类,我将如何实现这一点,我对EL表达式$ {var.index}有了很多了解,但是它如何工作? And how should i use this to dynamically add my Objects. 以及我应该如何使用它动态添加我的对象。 Edit2: Now im able to add single objects , and multiple objects to the list, but im not using any EL Expression, so i wanted to know when would i need to use something like this: Edit2:现在,我可以将单个对象和多个对象添加到列表中,但是我不使用任何EL表达式,所以我想知道何时需要使用如下所示的内容:

 <div th:each="feature,stat:*{featureArrayList}">
        <input  id="myInput" type="text" th:field="*{featureArrayList[__${stat.index}__].name}"/>
    </div>
  1. As far as I know, you can't bind to a list without a wrapper class (as stated in comments). 据我所知,没有包装类就无法绑定到列表(如注释中所述)。

  2. In normal Thymeleaf expressions, you can directly use variables as indexes. 在正常的Thymeleaf表达式中,您可以直接使用变量作为索引。 Expressions like this for example: 例如这样的表达式:

     <th:block th:with="i=0"> <span th:text="${array[i]}" /> </th:block> 

    However, th:field expressions are different. 但是,th:field表达式是不同的。 If you read the section on dynamic fields : 如果您阅读有关动态字段的部分:

    The problem is that Spring EL does not evaluate variables inside array index brackets, so when executing the above expression we would obtain an error telling us that rows[rowStat.index] (instead of rows[0] , rows[1] , etc) is not a valid position in the rows collection. 问题是Spring EL不会对数组索引括号内的变量求值,因此在执行上述表达式时,我们将得到一个错误,告诉我们rows[rowStat.index] (而不是rows[0]rows[1]等)在rows集合中不是有效位置。 That's why preprocessing is needed here. 这就是为什么这里需要预处理的原因。

    So, when you are using th:field to bind inputs to arrays, you must use preprocessing which is why you see expressions like this: 因此,当您使用th:field将输入绑定到数组时,必须使用预处理,这就是为什么看到以下表达式的原因:

     <input type="text" th:field="*{featureArrayList[__${stat.index}__].name}"/> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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