小编典典

如何在Spring Boot应用程序中使用Thymeleaf加载HashMap和ModelandView对象值?

spring-boot

我无法使用Thymeleaf在Spring-boot应用程序中从HTML文件从浏览器中加载输入值和实例值。

以下是Controller java文件中的代码段。

@RequestMapping(value = "/x")
public String launch(@RequestParam("inputFile") String inputFile, @RequestParam("instance") int instance) {

    ...
    ModelAndView mav = new ModelAndView();

    Map<String, String> parameters = new HashMap<>();

    parameters.put("inputFile", inputFile);
    parameters.put("instance", Integer.toString(instance));
    mav.addObject("parameters", parameters);

    mav.setViewName("welcome");

    return "welcome";
}

这是welcome.html的代码:

<!DOCTYPE HTML>
<html xmlns:th="http://www.thymeleaf.org">
<head>
    <title>Title</title>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
</head>
<body>
<div class="container">
    <div class="starter-template">
        <h1>Success!</h1>
        <h2><span th:text="inputFile: ${parameters.inputFile}"></span></h2>
        <h3><span th:text="instance: ${parameters.instance}"></span></h3>
    </div>
</div>
</body>
</html>

这是我在浏览器上遇到的错误:

无法解析为表达式:“ inputFile:$ {parameters.inputFile}”(欢迎:16)


阅读 754

收藏
2020-05-30

共1个答案

小编典典

请尝试以下解决方案之一。

解决方案1:

<table>
        <tr th:each="element : ${parameters}">
              <td th:text="${element.key}">keyvalue</td>
                    <table>
                        <tr th:each="elementSingle : ${element.value}">
                            <td th:text="${elementSingle.name}">Some name</td>
                            <td th:text="${elementSingle.description}">Description</td>
                        </tr>
                    </table>
        </tr>
    </table>

解决方案2:

<table class="table table-striped">
  <tr>
    <th>Board Name</th>
    <th>Investor</th>
    <th>Fuse Manufacturer</th>
    <th>Fuse Nr. Of Poles</th>
    <th>Fuse Characteritics</th>
    <th>Fuse Amount</th>
  </tr>

  <th:block th:each="item: ${map}">
    <tr th:each="fuse: ${item.value}">
      <td th:text="${item.key.name}" />
      <td th:text="${item.key.investor}" />
      <td th:text="${fuse.fuse.manufacturer.name}" />
      <td th:text="${fuse.fuse.type}" />
      <td th:text="${fuse.fuse.characteristics}" />
      <td th:text="${fuse.quantity}" />
    </tr>
  </th:block>
</table>

解决方案3:

<tr th:each="instance : ${parameters}">
                        <td th:text="${instance.key}">keyvalue</td>
                        <td th:text="${instance.value.fieldName}">num</td>
</tr>

注意:根据您的代码设置变量。

2020-05-30