I am trying to load drop down list using ArrayList in Springboot as i am new to springboot.
I have tried but not working as expected.
Please find the code which i have tried as below:
Java Code:
@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>
Finally no errors but only loading in dropdown list with --
and not loading the countries.
Please help me on this.