I am very new to coding in general, and have been learning Java. We've started to learn Spring Boot and Thymeleaf, and we have to work on an app where we search for jobs using keywords. I've created a handler to be able to search for the jobs and add them to an ArrayList
of a HashMap
, and have a template that displays those results. However, my issue is we're supposed to display the number of results each time at the top of the page, and I cannot figure out how to do that.
I've tried using .size
, #lists
, #maps
, th:size
, everything I can think of. Nothing is working. I've tried putting it above the table, in the table, between the loops, everything. The closest I've gotten is just printing the word "Results" at the top of the page. I've been reading Thymeleaf tutorials, looking at Baeldung, googling for days.
I very much appreciate any assistance!
@RequestMapping(value = "results")
public String search(
Model model,
@RequestParam String searchType,
@RequestParam String searchTerm
) {
ArrayList<HashMap<String, String>> results =
JobData.findByColumnAndValue(searchType, searchTerm);
model.addAttribute("columns", ListController.columnChoices);
model.addAttribute("results", results);
return "search";
}
And Thymeleaf:
<hr />
<!--<span th:text="${results.size}">Result</span> -->
<!--<span th:text="${#lists.size(job.results)}">Result(s)</span> -->
<!-- <td th:text="${#lists.size(job.results)}">Result(s)</td> -->
<!-- TODO #2 - Loop over jobs map to display all job fields -->
<!--<div th:fragment="div"> -->
<table class="job-listing" th:each="job : ${results}">
<tr th:each="row : ${job}">
<!--<td th:text="${results.size}"></td>-->
<td th:text="${row.key}"></td>
<td th:text="${row.value}"></td>
</tr>
</table>
</div>
<!-- </table>-->
<!--</div> -->
</body>
</html>
I've included my handler method, and the code from the template I'm working with. Sorry about all the comments - I'm trying to keep track of what I've tried.
Edit: This is now my template code that prints the word "Result(s)" only after I do a search (which I want). My issue now is I still can't get it to print the number of results.
<span th:if="${results} and ${results.size()}">
<h3>Result(s)</h3>
</span>
<table class="job-listing" th:each="job : ${results}">
<tr th:each="row : ${job}">
<td th:text="${row.key}"></td>
<td th:text="${row.value}"></td>
</tr>
</table>