【发布时间】:2019-09-24 18:01:59
【问题描述】:
我正在尝试在 Springboot 中使用 ArrayList 加载下拉列表,因为我是 springboot 新手。
我已经尝试过,但没有按预期工作。
请找到我尝试过的代码如下:
Java 代码:
@Controller
public class DemoController {
@GetMapping("/create")
public String create(Model model) {
model.addAttribute("create", new Demo());
return "create";
}
public void countriesList(Model model) {
List<String> countryList = new ArrayList<>();
countryList.add("US");
countryList.add("UK");
model.addAttribute("countries", countryList);
}
}
HTML:
<form action="#" th:action="@{/create}" th:object="${create}" method="post">
<select th:field="*{country}">
<option value=""> -- </option>
<option th:each="country : ${countries}" th:value="${country}" th:text="${country}"></option>
</select>
</form>
最后没有错误,但只在下拉列表中加载-- 而没有加载国家/地区。
请帮帮我。
【问题讨论】:
-
请添加您遇到的错误
-
我已经尝试过,但没有按预期工作并出现错误。 为什么你认为在你的问题中不包括这些错误是个好主意?
-
此外,您没有向模型添加任何内容。
-
如何添加模型..请帮助我,因为我是 Springboot 的新手。谢谢@Antoniossss
-
countryList必须添加到模型中,以便 thymeleaf 可以访问它。model.addAttribute("countries", countryList);
标签: java spring-boot thymeleaf