2

I use spring boot with thymeleaf 3

I try to display a bean

In my controller I have

public String getNewCarsTemplate(Model model) {
    model.addAttribute("cars", new Cars());
}       

In cars I have

@OneToOne
private Cities cities;  // field id and name

@OneToMany(mappedBy = "car")
private List<Locations> locations = new ArrayList<>();

In location I have

private Integer id;

private String name;

@ManyToOne
private Suppliers supplier;

In my thymeleaf fragment

<form id="carsForm" action="#" th:action="@{/template/new/cars}" th:object="${cars}" th:method="post">
    ...
    <input type="hidden" th:field="*{cities.id}" > 
   <input id="carsCities" class="form-control js-typeahead" type="search" placeholder="Type partial value" th:field="*{cities.name}" autocomplete="off" >
    ...
    <table id="locationsTable" class="table table-striped table-hover responsive">
        <thead>
            <tr>
                <th th:text="#{name}">Name</th>
                <th></th>
            </tr>
        </thead>
        <tbody>
            <tr th:each="car, stat : ${cars}">
                <td> 
                    <input type="hidden" th:id="${'locationId-'+stat.index}"  th:field="*{car.locations[__${stat.index}__].id}" />
                    <input type="text" class="form-control" th:id="${'locationName-'+stat.index}" th:placeholder="#{name.placeholder}" placeholder="Name" th:field="*{car.locations[__${stat.index}__].name}" />
                </td>
                <td class="align-middle"> <i class="fas fa-trash-alt"></i></td>
            </tr>
        </tbody>
    </table>

</form>

When I try to display this fragment I get

org.attoparser.ParseException: Exception evaluating SpringEL expression: "cities.id" Caused by: org.springframework.expression.spel.SpelEvaluationException: EL1008E: Property or field 'id' cannot be found on object of type 'java.util.ArrayList' - maybe not public or not valid?

robert trudel
  • 5,283
  • 17
  • 72
  • 124

1 Answers1

0

Could you try substituting

<input type="hidden" th:field="*{cities.id}" > 

with

<input type="hidden" th:field="*{cities?.id}" > 

and let me know if this works for you. The '?' operator will address the problem that in the new Cars() object the cities is null

Periklis Douvitsas
  • 2,431
  • 1
  • 13
  • 14