title: Thymeleaf: Working on and editing a List consisting of reference attributes
body: <p>I am currently struggeling on how to send a List with my RoomDTO class and alter them in Thymeleaf.</p>
  <p>My RoomDTO Class is defined as following:</p>
  <pre><code>@Getter
  @Setter
  public class RoomDTO {
  
      long roomNumber;
      String roomCategory;
      //LocaleDate as String as key
      Map&lt;String, String&gt; roomStateDays;
  
      public RoomDTO(){
  
      }
  
      public static RoomDTO fromRoom(Room room) {
          RoomDTO roomDTO = new RoomDTO();
          roomDTO.roomCategory = room.roomCategory().categoryName();
          roomDTO.roomNumber = room.roomNumber();
  
          roomDTO.roomStateDays = room.roomStateDays()
                  .stream()
                  .collect(Collectors.toMap(
                          (RoomStateDay rsd ) -&gt; rsd.date().toString(),
                          (RoomStateDay rsd) -&gt; rsd.state().name()
                  ));
  
          return roomDTO;
      }
  }
  
  </code></pre>
  <p>I have a Map that maps a LocalDate (which Thymeleaf did not like, so I converted it to a String) to a status. Now I want to set the status in a thymeleaf view.</p>
  <p>In my view, I select certain dates for different rooms and in a dropdown menu, I want to change the selection to a certain status. Since sending the list directly back did not work out well, I read that I could use a Wrapper Class to send the list back.
  In the Controller, I put the list into the wrapper, and sent it as a ModelAttribute to the View.</p>
  <p>Wrapper Class:</p>
  <pre><code>@Getter
  @Setter
  public class RoomsRequestWrapper {
  
    private List&lt;RoomDTO&gt; rooms;
  
    public RoomsRequestWrapper(List&lt;RoomDTO&gt; rooms) {
      this.rooms = rooms;
    }
  
  }
  </code></pre>
  <p>And in the Controller:</p>
  <pre><code>    @GetMapping(VIEW_ROOMS_URL)
      public String viewRooms(Model model){
          List&lt;RoomDTO&gt; rooms = roomService.getAllRooms();
          //gets first day of current week
          LocalDate now = LocalDate.now();
          TemporalField fieldISO = WeekFields.of(Locale.GERMANY).dayOfWeek();
          LocalDate firstDayOfWeek = now.with(fieldISO, 1);
          firstDayOfWeek = firstDayOfWeek.minusDays(1);
          RoomsRequestWrapper roomsWrapper = new RoomsRequestWrapper(rooms);
  
          model.addAttribute(&quot;title&quot;, &quot;Zimmerplan&quot;);
          model.addAttribute(&quot;firstDayOfSelectedWeek&quot;, now.with(fieldISO, 1));
          model.addAttribute(&quot;roomsWrapper&quot;, roomsWrapper);
          model.addAttribute(&quot;isCheckIn&quot;, false);
          return VIEW_ROOMS_VIEW;
      }
  
  </code></pre>
  <p>When being sent back, I either get an error that the mapping from String to List does not work or the list is just empty. Apparently Thymeleaf converts my list to a String attribute?</p>
  <p>Error Code:</p>
  <pre><code> WARN 4172 --- [nio-8080-exec-8] .w.s.m.s.DefaultHandlerExceptionResolver : Resolved [org.springframework.web.method.annotation.ModelAttributeMethodProcessor$1: org.springframework.validation.BeanPropertyBindingResult: 1 errors
  Field error in object 'roomsWrapper' on field 'rooms': rejected value [[at.fhv.team_e.Hotelverwaltung.application.dto.RoomDTO@42db53d3, at.fhv.team_e.Hotelverwaltung.application.dto.RoomDTO@4e53cb18, at.fhv.team_e.Hotelverwaltung.application.dto.RoomDTO@79b61c3c, at.fhv.team_e.Hotelverwaltung.application.dto.RoomDTO@20b3ce56, at.fhv.team_e.Hotelverwaltung.application.dto.RoomDTO@386e5458, at.fhv.team_e.Hotelverwaltung.application.dto.RoomDTO@68355346, at.fhv.team_e.Hotelverwaltung.application.dto.RoomDTO@212bf580, at.fhv.team_e.Hotelverwaltung.application.dto.RoomDTO@1fd44e8d, at.fhv.team_e.Hotelverwaltung.application.dto.RoomDTO@555215bb, at.fhv.team_e.Hotelverwaltung.application.dto.RoomDTO@173fd334, at.fhv.team_e.Hotelverwaltung.application.dto.RoomDTO@69c6b3a6, at.fhv.team_e.Hotelverwaltung.application.dto.RoomDTO@54db2835, at.fhv.team_e.Hotelverwaltung.application.dto.RoomDTO@11e110d8, at.fhv.team_e.Hotelverwaltung.application.dto.RoomDTO@782ed7a3, at.fhv.team_e.Hotelverwaltung.application.dto.RoomDTO@24a91083]]; codes [typeMismatch.roomsWrapper.rooms,typeMismatch.rooms,typeMismatch.java.util.List,typeMismatch]; arguments [org.springframework.context.support.DefaultMessageSourceResolvable: codes [roomsWrapper.rooms,rooms]; arguments []; default message [rooms]]; default message [Failed to convert value of type 'java.lang.String[]' to required type 'java.util.List'; nested exception is java.lang.IllegalStateException: Cannot convert value of type 'java.lang.String' to required type 'at.fhv.team_e.Hotelverwaltung.application.dto.RoomDTO': no matching editors or conversion strategy found]]
  </code></pre>
  <p>At this point I am really unsure, since I tried a lot of variations and every time I either get the convertion error or the list is just empty.</p>
  <p>Room status per day displayed in a table cell:</p>
  <pre><code>&lt;div class=&quot;day&quot; th:each=&quot;day: ${#numbers.sequence(0, 5)}&quot;&gt;
                          &lt;td colspan=&quot;2&quot; th:classappend=&quot;${room.roomStateDays.get(firstDayOfSelectedWeek.plusDays(day).toString())}&quot;&gt;
                              &lt;input hidden name=&quot;state&quot; th:field=&quot;*{roomsWrapper.rooms[__${iter.index}__].roomStateDays[__${firstDayOfSelectedWeek.toString()}__]}&quot;&gt;
                          &lt;/td&gt;
                      &lt;/div&gt;
  </code></pre>
  <p>Form to submit the changes:</p>
  <pre><code> &lt;div class=&quot;dropdown-menu&quot;&gt;
                      &lt;form method=&quot;post&quot; th:object=&quot;${roomsWrapper}&quot; th:action=&quot;@{/rooms/updateroomplan}&quot;&gt;
                          &lt;input th:field=&quot;*{rooms}&quot; th:value=&quot;${rooms}&quot; hidden&gt;
                          &lt;a type=&quot;submit&quot; class=&quot;dropdown-item&quot; onclick=&quot;fillOutDatesToUpdate('FREE', this.parentElement)&quot;&gt;frei&lt;/a&gt;
                      &lt;/form&gt;
                      &lt;form method=&quot;post&quot; th:object=&quot;${roomsWrapper}&quot; th:action=&quot;@{/rooms/updateroomplan}&quot;&gt;
                          &lt;input th:field=&quot;*{rooms}&quot; th:value=&quot;${rooms}&quot; hidden&gt;
                          &lt;a class=&quot;dropdown-item&quot; onclick=&quot;fillOutDatesToUpdate('BUSY', this.parentElement)&quot;&gt;belegt&lt;/a&gt;
                      &lt;/form&gt;
                      &lt;form method=&quot;post&quot; th:object=&quot;${roomsWrapper}&quot; th:action=&quot;@{/rooms/updateroomplan}&quot;&gt;
                          &lt;input th:field=&quot;*{rooms}&quot; th:value=&quot;${rooms}&quot; hidden&gt;
                          &lt;a class=&quot;dropdown-item&quot; onclick=&quot;fillOutDatesToUpdate('BEING_CLEANED', this.parentElement)&quot;&gt;wird geputzt&lt;/a&gt;
                      &lt;/form&gt;
                      &lt;form method=&quot;post&quot; th:object=&quot;${roomsWrapper}&quot; th:action=&quot;@{/rooms/updateroomplan}&quot;&gt;
                          &lt;input th:field=&quot;*{rooms}&quot; th:value=&quot;${rooms}&quot; hidden&gt;
                          &lt;a class=&quot;dropdown-item&quot; onclick=&quot;fillOutDatesToUpdate('MAINTENANCE', this.parentElement)&quot;&gt;Wartung&lt;/a&gt;
                      &lt;/form&gt;
  &lt;/div&gt;
  
  </code></pre>
  <p>Function that alters the highlighted/selected rooms on a certain date to the wished status (the rooms I want to change) via the hidden input field I created in the table cells:</p>
  <pre><code>    function fillOutDatesToUpdate(stateName, parent) {
          $('.highlighted').each(function(){
              $(this).children(&quot;:first&quot;).val(stateName);
          });
          parent.submit();
      };
  </code></pre>
  <p>After submitting the values I receive the wrapper via the model attribute.</p>
  <pre><code>  @PostMapping(UPDATE_ROOMS_URL)
      public ModelAndView updateRoomStates(@ModelAttribute(&quot;roomsWrapper&quot;) RoomsRequestWrapper rooms, Model model){
          //manipulation service to be implemented
          return new ModelAndView(&quot;redirect:&quot; + VIEW_ROOMS_URL);
      }
  </code></pre>
  <p>Is there any way to submit the changes or is not possible to work with those higher classes in Thymeleaf?</p>