java - 如何使用选择列表缩短代码? Spring HTML

标签 java html spring thymeleaf

我想知道如何缩短代码。我有一个用 HTML 编写的选择列表。这是机场列表。然而,它很长。太长了。

<body> <form action="#" th:action="@{/connect}" th:object="${FlightDTO}" method="post">

    <div class="container">
        <div class="main">

            <h2>Destination</h2>
            <select name="myselect" id="myselect"     th:field="*{departure}"   >
                <option value="AALAalesund">Aalborg</option>
                <option value="ZID">Aarhus</option>
                <option value="AAR">Aarhus</option>
                <option value="JEG">Aasiaat</option>
                <option value="ABD">Abadan</option>
    ... etc.

请帮助我。

最佳答案

您将机场添加到数据库中。

创建机场java类

Airport Class :

public class Airport {

    private String name;
    private String value;


    // setter and getter ...

}

并生成机场列表 List<Airport>并添加到model

model.addAttribute("airports", List<Airport> );

然后在 html 页面中你可以使用这个:

<option th:each="items : ${airports}" 
        th:value="${items.value}"
        th:text="${items.name}">
</option>

关于java - 如何使用选择列表缩短代码? Spring HTML,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56452147/

相关文章:

java - 在这种情况下选角如何进行?

java - XMLUnit : How to register a self-closing element as different to a non-self-closing element?

javascript - 灯箱图像调整大小错误

html - :hover doesn't work if hovered on :before pseudo generated content

java - Spring : Responding to a REST-ful API call

java - 如何使用 ApplicationContext 从 Spring 配置中获取集合

java - 更改java中字符串变量的输入

java - Apache POI 中行的自动大小高度

java - java.library.path 中没有 fontmanager

java - 在 Spring 配置中,用 XML 格式编写 Java 代码有什么好处?