使用Thymeleaf加载HashMap和ModelandVie

使用Thymeleaf加载HashMap和ModelandVie

本文介绍了如何在Spring Boot应用程序中使用Thymeleaf加载HashMap和ModelandView对象值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am unable to load the input and instance values in the browser from the HTML file using Thymeleaf for a Spring-boot application.

下面是Controller java文件中的代码段.

Below is code snippet from Controller java file.

@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中的代码:

Here is the code from 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>

这是我在浏览器中看到的错误:

Here is the error I get on the browser:

推荐答案

尝试以下解决方案之一.

Try one of the below solutions.

解决方案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>

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

NOTE: Set variable according to your code.

这篇关于如何在Spring Boot应用程序中使用Thymeleaf加载HashMap和ModelandView对象值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-26 06:55