I am new both to Spring Boot and Thymeleaf, and want to pass the value of a variable defined in the Java code to the HTML page. I searched the Web, but probably, I have overseen something important. I try to do it with the following code:
Favorite.java:
@Getter
@Setter
public class Favorite {
private String id;
private String target;
public Favorite(final String id, final String target) {
setId(id);
setTarget(this.target);
}
}
PortalController.java:
public class PortalController {
private final List<Favorite> myFavorites = new ArrayList<>();
@ModelAttribute("myFavorites")
public List<Favorite> myFavorites() {
if (myFavorites.size() == 0) {
myFavorites.add(new Favorite("ZEMPLOYEE_WORKTIME_ZWD_ESS_ABW", "ABC"));
myFavorites.add(new Favorite("ZEMPLOYEE_WORKTIME_CATS", "DEF"));
myFavorites.add(new Favorite("ZEMPLOYEE_WORKTIME_PEP_WISH_PLAN", "XYZ"));
}
return myFavorites;
}
index.html:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org" lang="de" xml:lang="de">
<head>
[…]
</head>
<body>
[…]
<ul>
<div th:switch="${not #lists.isEmpty(myFavorites)}">
<div th:case="true">
<div th:each="myFavorite : ${myFavorites}">
<li>
<td th:text="${myFavorite.id}"></td>
<td th:text="${myFavorite.task}"></td>
</li>
</div>
</div>
<div th:case="*">
Nothing to show!
</div>
</div>
</ul>
[…]
I get the "Nothing to show!" text, which means that myFavorites is empty. What do I miss or what did I misunderstood about this?
Edit:
I modified the PortalController after reading The @ModelAttribute in Depth to this:
public class PortalController {
private final List<Favorite> myFavorites = new ArrayList<>();
private final Map<String, List<Favorite>> favoritesMap = new HashMap<>();
@RequestMapping(value = "/getMyFavorites", method = RequestMethod.POST)
public String submit(@ModelAttribute("myFavorites") final List<Favorite> favorites,
final BindingResult result, final ModelMap model) {
if (result.hasErrors()) return "error";
model.addAttribute("myFavorites", favorites);
favoritesMap.put(favorites.toString(), favorites);
return "favoritesView";
}
@ModelAttribute
public void getMyFavorites(final Model model) {
if (myFavorites.size() == 0) {
myFavorites.add(new Favorite("ZEMPLOYEE_WORKTIME_ZWD_ESS_ABW", "ABC"));
[…]
}
model.addAttribute("myFavorites", myFavorites);
}
Unfortunately, there is still something missing or misunderstood by me so that the Web page returns still "Nothing to show!".
Edit 2:
This is the current state I have after reading the documentation requested here:
PortalController.java:
public class PortalController {
private final List<Favorite> myFavorites = new ArrayList<>();
@RequestMapping(value = "/getMyFavorites", method = RequestMethod.GET)
public String submit(@ModelAttribute("myFavorite") final List<Favorite> favorites,
final BindingResult result, final ModelMap model) {
if (result.hasErrors()) return "error";
model.addAttribute("myFavorites", favorites);
return "favoritesView";
}
@ModelAttribute
public void getMyFavorites(final Model model) {
if (myFavorites.size() == 0) {
myFavorites.add(new Favorite("ZEMPLOYEE_WORKTIME_ZWD_ESS_ABW", "ABC"));
[…]
}
model.addAttribute("myFavorites", myFavorites);
}
index.html:
<ul>
<div th:switch="${not #lists.isEmpty(myFavorite)}">
<div th:case="true">
<div th:each="myFavorite : ${myFavorites}">
<li>
<td th:text="${myFavorite.id}"></td>
<td th:text="${myFavorite.task}"></td>
</li>
</div>
</div>
<div th:case="false">
Nothing to show!
</div>
</ul>
But I still get "Nothing to show!", as ${myFavorites} is empty.
th:utext. When I did it exactly like you with theth:utextI got an array with object references back which was shown.print ${myFavorites}. And @CodeMatrix, how could I removeth:utext? It's not used anywhere.