esjii
esjii

Reputation: 1

How to show list of object in Thymeleaf in spring boot project?

I am new to Java and programming, I have just finished Java courses and now learning to use Springboot with thymeleaf and maven.

I can pass and show information with single objects but I cannot create and show the list containing these. I know this could be done with Repository but I am trying to figure this out without using it to understand the basics before moving on to repositories. I get Whitelabel error page, I know it's all wrong but I would really appreciate if someone could teach me how to do it as simple as possible. Thanks!

 public class FriendController {

     @RequestMapping(value="/hello", method=RequestMethod.GET)
     public String friendFrom (Model model) {
         model.addAttribute("friend", new Friend());
         return "/hello";
     }

     @RequestMapping(value="/hello", method=RequestMethod.POST)
     public String friendSubmit(Friend name, Model model) {
         model.addAttribute("friend", name);
         ArrayList<Friend> friends = new ArrayList<Friend>();
         friends.add(new Friend());
         return "/hello";
     }

}
<body>
    <h1>List of friends</h1>

    <table>
        <tr th:each="${friends}">
        <td th:text="${friend.name}">

        </tr>
    </table>

    <!-- This one worked on it's own -->
    <h1>ADD INFO</h1>
    <form action="#" th:action="@{/hello}" th:object="${friend}" method="post">
        <table>
        <tr>
            <td>ADD NAME: <input type="text" th:field="*{name}" /></td>
        </tr>
        </table>
        <p><input type="submit" value="Submit" /></p>
    </form>
</body>
</html>
public class Friend {

    private String name;



    public Friend() {
        super();
    }


    public Friend(String name) {
        super();
        this.name = name;
    }


    @Override
    public String toString() {
        return "Friend [name=" + name + "]";
    }


    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }




}

Upvotes: 0

Views: 7584

Answers (1)

Ismayil Ibrahimov
Ismayil Ibrahimov

Reputation: 598

you have two mistakes;

  1. add your friends list to model object in friendSubmit method:
@RequestMapping(value="/hello", method=RequestMethod.POST)
     public String friendSubmit(Friend name, Model model) {
         model.addAttribute("friend", name);
         ArrayList<Friend> friends = new ArrayList<Friend>();
         friends.add(new Friend());
         model.addAttribute("friends", friends); // add this line to your code
         return "/hello";
     }
  1. in thymeleaf:
<table>
        <tr th:each="friend : ${friends}"> // add friend
        <td th:text="${friend.name}"></td>
        </tr>
</table>

also if you need get submited object, you have to use @ModelAttribute, and instead of adding empty object to list, add @ModelAttribute's object to list:

@RequestMapping(value="/hello", method=RequestMethod.POST)
     public String friendSubmit(@ModelAttribute Friend friend, Model model) { // @ModelAttribute added
         model.addAttribute("friend", friend);
         ArrayList<Friend> friends = new ArrayList<Friend>();
         //friends.add(new Friend()); 
         friends.add(friend);
         model.addAttribute("friends", friends); // add this line to your code
         return "/hello";
     }

and you should add your list to session in order to keep alive your objects for all requests.

@RequestMapping(value="/hello", method=RequestMethod.POST)
     public String friendSubmit(@ModelAttribute Friend friend, Model model, 
                                HttpSession session) { // add HttpSession here
         model.addAttribute("friend", friend);
         ArrayList<Friend> friends = new ArrayList<Friend>();
         //friends.add(new Friend()); 
         friends.add(friend);
         //model.addAttribute("friends", friends);
         session.setAttribute("friends", friends); // add this line to your code
         return "/hello";
     }

in thymeleaf:

<table>
        <tr th:each="friend : ${session.friends}"> // add session variable
        <td th:text="${friend.name}"></td>
        </tr>
</table>

I hope this help you, good luck.

Upvotes: 4

Related Questions