0

i wanted to know, how Lists are getting binded in Spring...and how to access them in Thymeleaf. With and without WrapperClass How could achieve adding single / multiple objects through an form to an ArrayList or normal 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 ? 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:

 <div th:each="feature,stat:*{featureArrayList}">
        <input  id="myInput" type="text" th:field="*{featureArrayList[__${stat.index}__].name}"/>
    </div>
3

1 Answer 1

1
  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. Expressions like this for example:

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

    However, th:field expressions are different. 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. 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:

    <input type="text" th:field="*{featureArrayList[__${stat.index}__].name}"/>
    
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for your answer, finally i understood the usecase.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.