【问题标题】:Display object and list of object显示对象和对象列表
【发布时间】:2018-10-20 21:06:30
【问题描述】:

我使用带有百里香叶 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 的异常 表达式:“cities.id” 原因: org.springframework.expression.spel.SpelEvaluationException:EL1008E: 在类型的对象上找不到属性或字段“id” 'java.util.ArrayList' - 可能不公开或无效?

【问题讨论】:

  • 有趣...尝试在该输入标签上设置一些默认值

标签: thymeleaf


【解决方案1】:

你可以试试替换

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

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

如果这对你有用,请告诉我。 这 '?'运算符将解决 new Cars() 对象中城市为空的问题

【讨论】:

    猜你喜欢
    • 2020-03-22
    • 2013-11-22
    • 1970-01-01
    • 1970-01-01
    • 2019-04-17
    • 1970-01-01
    • 2016-07-22
    • 2013-01-14
    • 2019-02-13
    相关资源
    最近更新 更多