1

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>
Faheem azaz Bhanej
  • 2,328
  • 3
  • 14
  • 29
  • You send data to views in the model, maybe your question is how to access/use that model attribute in the view? – JorgeB Sep 20 '21 at 13:34
  • Yes, that is my question `how to access/use that model attribute in the view` but it is possible to send `List<>` in `Hashmap` value – Faheem azaz Bhanej Sep 20 '21 at 13:39
  • You can send via model attribute almost anything. You can use the map first looping through keys and then loop the list – JorgeB Sep 20 '21 at 13:43
  • Does this answer your question? [How to loop through Map in Thymeleaf](https://stackoverflow.com/questions/23144358/how-to-loop-through-map-in-thymeleaf) – JorgeB Sep 20 '21 at 13:44
  • But it is possible to send List<> in Hashmap value from controller – Faheem azaz Bhanej Sep 20 '21 at 13:46
  • 1
    Yes you can send `Map>` in model attribute – JorgeB Sep 20 '21 at 13:48
  • If you `return "redirect:/";` your model is lost, in that scenario you must use [redirect attributes](https://stackoverflow.com/questions/49626038/how-to-pass-the-model-as-a-redirect-attribute-in-spring) – JorgeB Sep 21 '21 at 06:37
  • my problem is not with name problem is how to send data to view – Faheem azaz Bhanej Sep 21 '21 at 10:00
  • I see you have changed the redirect, what name do yo refer? You send data to the view putting it in the model, there is no mystery. Please explain/post the full error you are having – JorgeB Sep 21 '21 at 10:30
  • `return "post" // page name` and i have no error i don't know how to pass to this data to view – Faheem azaz Bhanej Sep 21 '21 at 10:32
  • There are differences between doing a `redirect` or returning a page. Please post the full stacktrace of the error. – JorgeB Sep 21 '21 at 10:34
  • i know very very well the difference between them, my problem is how to pass `userUploadPost.put(getUserForPost.getReceiver_id(), getUserWithPost);` to view – Faheem azaz Bhanej Sep 21 '21 at 10:38
  • With this line `mdl.addAttribute("userWithPost", userUploadPost);` you are putting in the model that object, from java point o view there is nothing more to do, that is why i ask you to post the full stacktrace because the problem, i guess, is generating in the view – JorgeB Sep 21 '21 at 10:40

1 Answers1

3

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>
João Dias
  • 16,277
  • 6
  • 33
  • 45
JorgeB
  • 103
  • 2
  • 9