Domanda

Sto cercando di creare un frammento ricorsivo in TyMuleaf per produrre un menu sul lato sinistro.Core Architecture è Boot Spring, e sto passando in un oggetto menu popolato con bambini e tali.Codice per il menu Sembra:

public class MenuItem {
    private String displayLabel;
    private String actionUri;

    private List<MenuItem> children;

    // ...getters/setters/etc...
}
.

e un frammento di tymeleaf definito come:

<!DOCTYPE HTML>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<title>Menu</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
</head>
<body>
    <div th:fragment="submenu (menu)" th:remove="tag">
        <ul th:if="${not #lists.isEmpty(menu.children)}" th:class="${#objects.nullSafe(depth, 1) &gt; 1}? 'children'">
            <li th:each="child : ${menu.children}">
                <span th:if="${#strings.isEmpty(child.actionUri)}" th:text="${child.displayLabel}">Addons</span>
                <a th:if="${not #strings.isEmpty(child.actionUri)}" th:href="${child.actionUri}" th:text="${child.displayLabel}">Link Item</a>
                <div th:replace="fragments/submenu :: submenu(menu=${child}, depth=depth+1)" th:remove="tag" />
            </li>
        </ul>
    </div>
</body>
</html>
.

Se prendo il codice depth, tutto sembra funzionare bene.Ma con esso ottengo:

2014-07-30 11:13:00.832 ERROR 45869 --- [nio-8080-exec-4] o.a.c.c.C.[.[.[/].[dispatcherServlet]    : Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is org.thymeleaf.exceptions.TemplateProcessingException: Exception evaluating SpringEL expression: "#objects.nullSafe(depth, 1) > 1" (fragments/submenu:9)] with root cause

java.lang.ClassCastException: java.lang.Integer cannot be cast to java.lang.String
.

Lo scopo del codice depth è così posso opzionalmente inserire in una classe da tracciare quando le voci del menu sono figli del genitore.È possibile fissare il contatore di profondità o fare qualcos'altro da testare se il frammento è stato già corso?Alla fine voglio solo un menu ricorsivo con il top ul non avendo alcuna classe e il bambino uls avendo una classe di children.

È stato utile?

Soluzione

prova queste modifiche

1. Utilizzare due parametri in frammento come

<div th:fragment="submenu (menu, depth)" >
.

2.Passare la profondità come

<div th:replace="fragments/submenu :: submenu(menu=${child}, depth=${depth+1})"  />
.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top