[英]How to populate a combo box with an ArrayList in Thymeleaf?
我有一個包含對象的ArrayList,我想用arraylist中的每個對象(主題)填充下拉框。
目前,我的代碼是將ArrayList添加到組合框中,但是它在每行的ArrayList中顯示所有內容,而不是每行顯示一個主題。 (見下圖)
如何獲取它以顯示arraylist中的每個對象?
我的控制器
@GetMapping("/addexam")
public String showExamForm(Model model) {
Authentication loggedInUser = SecurityContextHolder.getContext().getAuthentication();
String email = loggedInUser.getName();
User user = userRepository.findByEmailAddress(email);
ArrayList<String> subjects = new ArrayList<String>();
for (Subject sub : user.getSubject()) {
subjects.add(sub.getSubjectName());
}
model.addAttribute("subjects", subjects);
return "addExam";
}
HTML文件
<select class="form-control" id="subjectOrder" name="subjectOrder">
<option value="">Select subject</option>
<option th:each="Subject : ${subjects}" th:value="${subjects}" th:text="${subjects}"></option>
</select>
您在html部分似乎是錯誤的,因為您正在使用位於th:value
和th:text
的整個列表(因此獲取每一行中的所有條目)。
應該
<option th:each="Subject : ${subjects}"
th:value="${Subject}"
th:text="${Subject}"></option>
海事組織。 請注意,使用${Subject}
而不是${subject[s]}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.