【问题标题】:Thymeleaf can't see For-each variableThymeleaf 看不到 For-each 变量
【发布时间】:2021-04-12 00:11:03
【问题描述】:

这是我的服务(它只是获取实体列表)

  @Service
    public class ProcedureService {
        @Autowired
        ProcedureRepository procedureRepository;
    
    
        public List getProceduresList() {
            List<Procedure> procedureList;
            procedureList = procedureRepository.findAll();
            return procedureList;
        }
    
    }

这是我的控制器。它只是列出他可以查看的列表。

    @Controller
public class ServicesController {
    @Autowired
    ProcedureService procedureService;


@GetMapping("/services")
public String getCustomer(Model model) {
    List<Procedure> procedures = procedureService.getProceduresList();
        model.addAttribute("procedures", procedures);
        return "services";
        }
}

这是我真正的问题。百里香叶只是看不到列表中的实体(尽管它看到了列表)。这是我的屏幕和完整的查看代码

    <!DOCTYPE html>
<html lang="en">
<head>
  <title>SpringMVC + Thymeleaf + Bootstrap 4 Table Example</title>
  <meta charset="utf-8"/>
  <meta name="viewport" content="width=device-width, initial-scale=1"/>
  <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css"/>
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.12.9/umd/popper.min.js"></script>
  <script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/js/bootstrap.min.js"></script>
</head>

<body>
<div class="container">
  <h1>Customer Table</h1>
  <div class="row col-md-7 table-responsive">
    <table id="customerTable" class="table">
      <thead>
      <tr>
        <th>Id</th>
        <th>Name</th>
        <th>Age</th>
        <th>Street</th>
        <th>Postcode</th>
      </tr>
      </thead>
      <tbody>
      <tr th:each="procedure: ${procedures}">
        <td th:text="${procedure.}" />
        <td th:text="${procedure.getId}" />
      </tr>
      </tr>
      </tbody>
    </table>
  </div>
</div>

【问题讨论】:

  • 嗨欧文。在您的屏幕截图中,我认为您在包含错误的表格行之后有一个胭脂&lt;/tr&gt;。删除它有帮助吗?
  • @RichardWoods 谢谢,我已经编辑过了,但还是看不到实体
  • 嗯...错误突出显示是由您的 IDE(intellij?)而不是 thymeleaf 添加的,您在运行代码时是否收到错误消息?如果不是,则可能是 Intellij 主题插件的问题。

标签: spring-boot thymeleaf


【解决方案1】:

我忘记了 html 中的 html lang="en" xmlns:th="http://www.thymeleaf.org" 所以它并没有真正起作用。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-07-22
    • 2018-09-27
    • 2017-09-09
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多