How do I load HashMap and ModelandView object values using Thymeleaf in a Spring Boot application?

余生长醉 提交于 2020-05-15 05:07:06

问题


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

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";
}

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:

Could not parse as expression: "inputFile: ${parameters.inputFile}" (welcome:16)


回答1:


Try one of the below solutions.

Solution 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>

Solution 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>

Solution 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.




回答2:


From the Question you posted, you're creating a new HashMap object and without adding any value to it you're sending it to your thymeleaf page.

However if you pasted the code just for an example to illustrate your problen then Try this Approach on your thymleaf-HTML page :--

<c:forEach items="${contactForm.contactMap}" var="contactMap" varStatus="status">
        <tr>
            <td>${contactMap.key}</td>
            <td><input name="contactMap['${contactMap.key}']" value="${contactMap.value}"/></td>
        </tr>
    </c:forEach>


Please Note :- I've got the solution from this page, go through it, it has the solve the same thing on which you're stuck :-
https://viralpatel.net/blogs/spring-mvc-hashmap-form-example/



来源:https://stackoverflow.com/questions/51317170/how-do-i-load-hashmap-and-modelandview-object-values-using-thymeleaf-in-a-spring

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!