【问题标题】:Spring mvc with Thymeleaf and bootstrap. Cant resolve model atribute带有 Thymeleaf 和引导程序的 Spring mvc。无法解析模型属性
【发布时间】:2020-03-23 08:54:27
【问题描述】:

在浏览器中按地址:“localhost:8081/transports”我得到信息:传输列表。

我在Fuel 列有问题,有整数值,FK 数据到燃料表。有人可以帮我在表格中查看燃料名称吗,现在我得到了:

audi | 1 | 55 | 2020-01-01

但我想得到:

audi | diesel | 55 | 2020-01-01

html模板表格部分:

<section id="transport">
    <div class="container-fluid">
        <div class="row">
            <div class="col">
                <div class="card">
                    <table class="table table-striped">
                        <thead class="thead-dark">
                        <tr>
                            <th>Name</th>
                            <th>Fuel</th>
                            <th>Tank capasity</th>
                            <th>Date</th>
                            <th style="width: 10%"></th>
                            <th style="width: 10%"></th>
                        </tr>
                        </thead>

                        <tbody>
                        <tr th:each="transport:${transports}">
                            <td th:text="${transport.transportName}">Golf-Variant-CL-1.8(5M)</td>
                            <td th:text="*{transport.fuelId}">Gasoline</td>
                            <td th:text="${transport.transportTankCapasity}">45</td>
                            <td th:date="${transport.transportDate}">2020-02-04</td>
                            <td class="text-right">
                                <a href="transport.html"
                                   th:href="@{|/transport/${transport.transportId}|}"
                                   class="btn-sm btn-outline-secondary"
                                   title="Edit transport"
                                   data-toggle="tooltip"
                                   data-placement="top">
                                    <i class="fa fa-pencil"></i>
                                    <span class="d-none d-md-inline">Edit</span>
                                </a>
                            </td>
                            <td class="text-left">
                                <span data-toggle="modal" data-target="#deleteDialog">
                                <a href="#"
                                   class="btn-sm btn-outline-secondary"
                                   title="Delete transport"
                                   data-toggle="tooltip"
                                   data-placement="top">
                                    <i class="fa fa-remove"></i>
                                    <span class="d-none d-md-inline">Delete</span>
                                </a>
                                </span>
                            </td>
                        </tr>                          
                        </tbody>
                    </table>
                </div>
            </div>
        </div>
    </div>
</section>

MVC 控制器代码:

/**
     *  Goto transports list page.
     * @param model model.
     * @return view name.
     */
    @GetMapping(value = "/transports")
    public String transports(Model model) {
        LOGGER.debug("transports()");
        model.addAttribute("transports", transportService.findAll());
        model.addAttribute("fuels", fuelService.findAll());
        return "transports";
    }

创建数据库代码:

drop table if exists transport;
drop table if exists fuel;

CREATE TABLE fuel (
  fuel_id   INTEGER NOT NULL AUTO_INCREMENT,
  fuel_name VARCHAR(30) NOT NULL,
  PRIMARY KEY (fuel_id)
);

CREATE TABLE transport (
  transport_id    INTEGER NOT NULL AUTO_INCREMENT,
  transport_name VARCHAR(30) NOT NULL,
  fuel_id   INTEGER NOT NULL,
  transport_tank_capasity INTEGER(3) NOT NULL DEFAULT 0,
  transport_date Date NOT NULL,
  PRIMARY KEY (transport_id),
  CONSTRAINT transport_to_fuel_fk
    FOREIGN KEY (fuel_id)
    REFERENCES fuel (fuel_id)
    ON DELETE NO ACTION
    ON UPDATE NO ACTION
);

【问题讨论】:

    标签: spring model-view-controller thymeleaf


    【解决方案1】:

    要将变量值插入到thymeleaf 方法中,您需要使用__${someValue}__ 构造。见例子:

    <td th:text="${fuels.get(__${transport.fuelId}__).getFuelName()}">Gasoline</td>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-02-20
      • 1970-01-01
      • 2013-08-27
      • 2021-10-10
      • 2017-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多