[英]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.