提问者:小点点

如何使用Java和Thymeleaf将变量值传递给超文本标记语言


我是Spring Boot和Thymeleaf的新手,想将Java代码中定义的变量的值传递给超文本标记语言页面。我在网上搜索,但可能,我已经监督了一些重要的东西。我尝试使用以下代码:

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为空。我错过了什么或者我对此有什么误解?

编辑:

我在深入阅读了The@Model属性后修改了PortalController

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

不幸的是,我仍然缺少或误解了一些东西,因此网页仍然返回“无显示!”。

编辑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>

但我仍然得到“没什么可展示的!”,因为${myFavorites}是空的。


共1个答案

匿名用户

根据留档,@Model属性应该与@Request estMaps一起使用。您可以在此处找到有关此注释如何工作的更详细描述。

同样为了显示表格,您可以将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>