Home > Software design >  How to send data using HASHMAP
How to send data using HASHMAP

Time:09-22

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(K,V) is List<>, I got error like that...

Error:

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.

Example:

Map<Integer,List<Data>> Userdata = new HashMap<>();

Query:

    @Query(nativeQuery = true, value="SELECT  * FROM request_master WHERE sender_id = ? AND status = ?")
    List<requestEntity> getAcceptRequestFrnd(int 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<profileJoinDto> getPostWithAccount(int Receiver_id);

Controller:

@RequestMapping(value = "/", method = RequestMethod.GET)
    public String home(Model mdl, HttpSession session, requestEntity requestEntity) {
        Integer SessionId = Integer.parseInt(session.getAttribute("uid").toString());
        Map<Integer, List<profileJoinDto>> userUploadPost = new HashMap<>();
        List<requestEntity> getUser = 
        this.meetzenService.getAcceptRequestFrnd(SessionId, "Accept");
        for(requestEntity getUserForPost : getUser)
        {
            List<profileJoinDto> getUserWithPost = this.meetzenService.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>

CodePudding user response:

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>
  • Related