Spring Boot Thymleaf mit Java.Optional

jCoder1984

Aktives Mitglied
Hallo zusammen,
ich bin mir nicht sicher ob es das richtige Forum ist - aber ich versuche mal mein Glück.
Also ich möchte gerne eine Spring boot Application mit Thymleaf erstellen. Das klappt auch schon. Nur wenn ich ein Object darstellen möchte, das ein OPtional Property hat komme ich nicht weiter. Hier mal mein konkrter Fall :

Auszug aus der index.html :
HTML:
    <!-- Main component for a primary marketing message or call to action -->
    <div class="jumbotron">

        <table class="table table-bordered table-hover table-striped" th:if="${not #lists.isEmpty(matchList)}">
            <tr>
                <th>Datum</th>
                <th>Uhrzeit</th>
                <th>Wettbewerb</th>
            </tr>
            
            <tr th:each="match : ${matchList}">
                <td th:text="${#temporals.format(match.dateTime, 'dd.MM.yyyy')}"></td>
                <td th:text="${#temporals.format(match.dateTime, 'HH:mm')}"></td>
                <td th:text="${match.matchdayRef.get.name}"></td>
            </tr>


        </table>
    </div>

Auszug aus dem MaiController :

Java:
@Controller
public class MainController {

    @Autowired
    private IManager manager;

    @GetMapping("/")
    public String index(Model model) {
        model.addAttribute("seasonList", manager.getObjectList().stream().filter(o -> o instanceof ISeason).collect(Collectors.toList()));
        model.addAttribute("associationList", manager.getObjectList().stream().filter(o -> o instanceof IAssociation).collect(Collectors.toList()));
        model.addAttribute("competitionList", manager.getObjectList().stream().filter(o -> o instanceof ICompetition).collect(Collectors.toList()));
        model.addAttribute("matchList", manager.getObjectList().stream().filter(o -> o instanceof IMatch).collect(Collectors.toList()));

        return "index";
    }

Und noch das Match Interface :

Java:
public interface IMatch<T extends IMatchday> extends IEvent {

    /**
     * @return the reference of the {@link IMatchday} object
     */
    default Optional<T> getMatchdayRef() {
        return Optional.empty();
    }

So nun meine konrete Frage : ich möchte in der index.html eine liste von Match Objekten darstellen und zu jedem Match Object auch den Matchdaynamen. Dazu muss ich aber das Matchday Object zuerst von dem Optional holen.

Wie erledigt man dies in Thymleaf
 

Zurück
Oben