title: How to use spring mvc with thymeleaf to iterate over a list?
body: <p><em>My goal is to cycle through a list of objects, some to be displayed on the screen, others to be passed into a form as an object of which I can define certain aspects and then return to the controller the object and attribute to be modified.</em></p>
  <p><em>The problem with the following approach is that the object in the list is not passed correctly to the form and thus gives an error because it is trying to make changes to a non-existent object.</em></p>
  <p>If, on the other hand, I try to pass it as an object via ModelAndView it obviously works but does not have all the characteristics of the object I passed via the list.</p>
  <p><strong>Controller</strong></p>
  <pre><code>@GetMapping(&quot;/&quot;)
      public ModelAndView home() throws IOException {
          ModelAndView mv = new ModelAndView();
          mv.setViewName(&quot;home&quot;);
  
          List&lt;Comics&gt; allComics = cs.getAll();
          
          mv.addObject(&quot;comics&quot;, allComics);
          return mv;
      }
  
  @PostMapping(&quot;/update&quot;)
      public ModelAndView update(Comics com, @RequestParam(&quot;attr&quot;) String attr) throws IOException {
          ModelAndView mv = new ModelAndView();
          
          com.setLastRead(attr);
          
          cs.updateAttributes(com);
          
          mv.setViewName(&quot;home&quot;);
          List&lt;Comics&gt; allComics = cs.getAll();
          mv.addObject(&quot;comics&quot;, allComics);
          return mv;
      }
  </code></pre>
  <p><strong>home.html</strong></p>
  <pre><code>&lt;html xmlns:th=&quot;http://www.thymeleaf.org&quot;&gt;
  &lt;tr th:each=&quot;comic : ${comics}&quot;&gt;
                      &lt;td th:text=&quot;${comic.title}&quot;&gt;&lt;/td&gt;
                      &lt;td th:text=&quot;${comic.lastChapter}&quot;&gt;&lt;/td&gt;
                      &lt;td&gt;
                      &lt;a th:href=&quot;${comic.lastChapterLink}&quot; target=&quot;_blank&quot;
                      role=&quot;button&quot; class=&quot;btn btn-md btn-block btn-info&quot;&gt; Link
                      &lt;/a&gt;
                      &lt;/td&gt;
                          &lt;td&gt;&lt;/td&gt;
                      &lt;td&gt;
                          &lt;form th:action=&quot;@{/update}&quot; th:object=&quot;${comic}&quot; method=&quot;post&quot;&gt;
                              &lt;input type=&quot;text&quot; name=&quot;attr&quot; id=&quot;attr&quot;/&gt;
                              &lt;button type=&quot;submit&quot;&gt;Sub&lt;/button&gt;
                          &lt;/form&gt;
                      &lt;/td&gt;
  &lt;/tr&gt;
  </code></pre>
  <p>PS: I cut out the head of the html page because it was full of non-relevant CDNs</p>
  <p><em>How can I integrate Spring MVC with Thymeleaf to achieve the result whereby passing a list of objects can be displayed on the screen and used for other purposes within the html page without throwing errors?</em></p>
  <p>Obviously if you know of more efficient methods to achieve the result I'm listening; I only used this method because I didn't know of any others.</p>
  <p>Thank you</p>
  <p><strong>Answer to @RafaeldaSilva:</strong></p>
  <p>I agree, but that does not solve the problem.
  Let me explain: the attribute I am going to modify through the form already has its name to allow what you wrote.
  But the object iterated through:</p>
  <pre><code>tr th:each=&quot;comic : ${comics}&quot;&gt;
  </code></pre>
  <p>cannot be passed directly as input, as it is a value that is taken from a list and exists individually only in the html page.
  One might think of passing it as hidden input, but in this case the result would be the same (I have tried):</p>
  <pre><code>&lt;form th:action=&quot;@{/update}&quot; th:object=&quot;${comic}&quot; method=&quot;post&quot;&gt;
                              &lt;input type=&quot;hidden&quot; value=&quot;${comic}&quot; name=&quot;com&quot;/&gt;
                              &lt;input type=&quot;text&quot; name=&quot;attr&quot; id=&quot;attr&quot;/&gt;
                              &lt;button type=&quot;submit&quot;&gt;Sub&lt;/button&gt;
  &lt;/form&gt;
  
  @PostMapping(&quot;/update&quot;)
      public ModelAndView update(@RequestParam(&quot;com&quot;) Comics com, @RequestParam(&quot;attr&quot;) String attr) throws IOException {
          ModelAndView mv = new ModelAndView();
          
          com.setLastRead(attr);
          
          System.out.println(&quot;Comic: &quot; + com);
          
          cs.updateAttributes(com);
          
          mv.setViewName(&quot;home&quot;);
          List&lt;Comics&gt; allComics = cs.getAll();
          mv.addObject(&quot;comics&quot;, allComics);
          return mv;
      }
  </code></pre>
  <p><strong>Error:</strong>
  [org.springframework.web.bind.MissingServletRequestParameterException: Required request parameter 'com' for method parameter type Comics is present but converted to null]</p>