3

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)

Mahozad
  • 18,032
  • 13
  • 118
  • 133
Tito
  • 87
  • 1
  • 11
  • 1.) Your issue is quite similar to this :-- https://stackoverflow.com/questions/42932572/thymeleaf-format-for-list-within-a-hashmap – Sumit Jul 13 '18 at 13:37

2 Answers2

3

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.

Alien
  • 15,141
  • 6
  • 37
  • 57
  • Thanks for your suggestion. When I tried with it, I got the output as below: Key: ${parameter.key} - Value: ${parameter.value} – Tito Jul 13 '18 at 15:31
  • sorry bro..i missed the word thymeleaf and given solution for JSPs using JSTL. But now updated my answer for thymeleaf and it should work fine. – Alien Jul 13 '18 at 15:49
  • No problem! Thank you for the fast response. I will try it rightaway and let you know. – Tito Jul 13 '18 at 15:51
  • Sorry to let you know that, it failed. I just got printed with "Success!" I tried with solution 1 and nothing in code got printed. – Tito Jul 13 '18 at 16:42
  • To try solution 3, I am not able to co-relate analysis & instanceMap in your example to my code. Can you please help me? I know I am close to get it working. – Tito Jul 13 '18 at 16:43
  • you just put "parameters" not "analysis.instanceMap"..updated answer check once. – Alien Jul 13 '18 at 16:49
  • 1
    Thanks for your guidance. I am trying it out now! Keep you posted! – Tito Jul 13 '18 at 18:26
  • I tried with Solution 3 as suggested, nothing was printed, like last time. – Tito Jul 13 '18 at 18:38
  • Thank you so much @Alien. Your solution #3 worked for me. I had to tie parameters to the model object in the controller class, without that I cannot use parameters in the .html file. – Tito Jul 17 '18 at 18:51
0

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/

Sumit
  • 917
  • 1
  • 7
  • 18
  • Thanks Sumit for your suggestion. I couldn't translate your example to my code. Can you help me with it? – Tito Jul 13 '18 at 15:35