显示对象和对象列表

时间:2018-05-10 14:40:46

标签: thymeleaf

我使用带百里香叶3的春季靴子

我尝试显示一个bean

在我的控制器中我有

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

我有汽车

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

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

我所在的位置

private Integer id;

private String name;

@ManyToOne
private Suppliers supplier;

在我的百里香片中

<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>

当我尝试显示这个片段时,我得到了

  

org.attoparser.ParseException:评估SpringEL的异常   表达:&#34; cities.id&#34;引起:   org.springframework.expression.spel.SpelEvaluationException:EL1008E:   财产或字段&#39; id&#39;在类型的对象上找不到   &#39;的java.util.ArrayList&#39; - 可能不公开或无效?

1 个答案:

答案 0 :(得分:0)

你能尝试替代

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

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

让我知道这是否适合你。 &#39;?&#39;运算符将解决新的Cars()对象中的城市为空的问题