首页 > 解决方案 > 如何使用 Java 和 Thymeleaf 将变量值传递给 HTML

问题描述

我是 Spring Boot 和 Thymeleaf 的新手,并且想将 Java 代码中定义的变量的值传递给 HTML 页面。我在网上搜索过,但很可能,我监督了一些重要的事情。我尝试使用以下代码来做到这一点:

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>
        […]

我得到“没什么可显示的!” 文本,这意味着它myFavorites是空的。我错过了什么或者我对此有什么误解?

编辑:

PortalController在阅读The @ModelAttribute in Depth后将其修改为:

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);
}

不幸的是,我仍然缺少或误解了某些内容,因此网页仍然返回“Nothing to show!”。

编辑2:

这是我阅读此处要求的文档后的当前状态:

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>

但我仍然得到“Nothing to show!”,因为${myFavorites}它是空的。

标签: javaspring-bootthymeleaflombok

解决方案


根据文档@ModelAttribute 应该与@RequestMapping 一起使用。您可以在此处找到有关此注释如何工作的更详细说明。

此外,为了显示表格,您可以将 thymeleaf 代码更改为:

<table>
    <thead>
        <tr>
            <th> id</th>
            <th> target</th>
        </tr>
    </thead>
    <tbody>
    <tr th:if="${myFavorites.empty}">
            <td colspan="2"> No Info </td>
        </tr>
        <tr th:each="myFavorite: ${myFavorites}">
            <td><span th:text="${myFavorite.id}"> ID</span></td>
            <td><span th:text="${myFavorite.target}"> Target</span></td>
        </tr>
    </tbody>
</table>

推荐阅读