[英]Dropdown list using Thymeleaf in SpringBoot
I am trying to load drop down list using ArrayList in Springboot as i am new to springboot. 我正在尝试使用Springboot中的ArrayList加载下拉列表,因为我是springboot的新手。
I have tried but not working as expected. 我已经尝试过,但是没有按预期工作。
Please find the code which i have tried as below: 请找到我尝试过的代码如下:
@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);
}
}
<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>
Finally no errors but only loading in dropdown list with --
and not loading the countries. 最终没有错误,仅使用--
加载下拉列表,而不加载国家/地区。
Please help me on this. 请帮我。
You never called your function for adding countries to the model... 您从未调用过将模型添加国家/地区的功能...
@Controller
public class DemoController {
@GetMapping("/create")
public String create(Model model) {
model.addAttribute("create", new Demo());
countriesList(model); // this line is needed
return "create";
}
private void countriesList(Model model) {
List < String > countryList = new ArrayList < > ();
countryList.add("US");
countryList.add("UK");
model.addAttribute("countries", countryList);
}
}
try with this code block 尝试使用此代码块
<span th:each="country:${countries}" th:remove="tag">
<option th:value="${country}" th:text="${country}"></option>
</span>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.