Issue
I want to get data form repository to controller and send them into view so, I want to send data from controller to view using Hashmap(K,V)
. But value of Hashmap(V)
is List<>
, I got error like that...
Stack trace
Cannot render error page for request [/meetzen/] and exception [An error happened during template parsing (template: "class path resource [templates/post.jsp]")] as the response has already been committed. As a result, the response may have the wrong status code.
Query
@Query(nativeQuery = true, value="SELECT * FROM request_master WHERE sender_id = ? AND status = ?")
List<Request> getAcceptRequestFrnd(Integer Sender_id, String Status);
@Query(nativeQuery = true, value="SELECT rsm.receiver_id,pm.profile, rgm.username, upm.post_id, upm.post, upm.date FROM registration_master AS rgm INNER JOIN profile_master AS pm ON rgm.u_id = pm.user_id INNER JOIN uploadpost_master AS upm ON rgm.u_id = upm.user_id INNER JOIN request_master AS rsm ON rgm.u_id = rsm.receiver_id WHERE rsm.receiver_id = ? ORDER BY upm.date DESC LIMIT 1")
List<ProfileJoin> getPostWithAccount(Integer Receiver_id);
Controller
@RequestMapping(value = "/", method = RequestMethod.GET)
public String home(Model mdl, HttpSession session, Request request) {
Integer sessionId = Integer.parseInt(session.getAttribute("uid").toString());
Map<Integer, List<ProfileJoin>> userUploadPost = new HashMap<>();
List<Request> getUser =
this.service.getAcceptRequestFrnd(sessionId, "Accept");
for(Request getUserForPost : getUser)
{
List<ProfileJoin> getUserWithPost = this.service.getPostWithAccount(getUserForPost.getReceiver_id());
userUploadPost.put(getUserForPost.getReceiver_id(), getUserWithPost);
}
mdl.addAttribute("userWithPost", userUploadPost);
mdl.addAttribute("getUser", getUser);
return "index";
}
Thymeleaf:
<div th:each="getData: ${getUserWithPost.value}">
<span th:text="${getData.username}"></span>
<span th:text="${getData.post}"></span>
</div>
Solution
When you have a map with key being the category, and value being a list of items pertaining to that category, you can use this:
<table>
<tr th:each="element : ${catsAndItems}">
<td th:text="${element.key}">keyvalue</td>
<table>
<tr th:each="anews : ${element.value}">
<td th:text="${anews.title}">Some name</td>
<td th:text="${anews.description}">Some name</td>
<td th:text="${anews.url}">Some name</td>
<td th:text="${anews.logo}">Some name</td>
<td th:text="${anews.collectionDate}">Some name</td>
</tr>
</table>
</tr>
</table>
Answered By - JorgeB