Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何在Thymeleaf中使用ArrayList填充组合框?_Java_Spring_Spring Boot_Thymeleaf - Fatal编程技术网

Java 如何在Thymeleaf中使用ArrayList填充组合框?

Java 如何在Thymeleaf中使用ArrayList填充组合框?,java,spring,spring-boot,thymeleaf,Java,Spring,Spring Boot,Thymeleaf,我有一个包含对象的ArrayList,我想用ArrayList中的每个对象(主题)填充下拉框 目前,我的代码正在将ArrayList添加到组合框中,但是它在每行显示ArrayList中的所有内容,而不是每行显示一个主题。(见下图) 如何让它显示arraylist中的每个对象 我的控制器 @GetMapping("/addexam") public String showExamForm(Model model) { Authentication loggedInUser = Secur

我有一个包含对象的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";
}
@GetMapping(“/addexam”)
公共字符串showExamForm(模型){
身份验证loggedInUser=SecurityContextHolder.getContext().getAuthentication();
字符串email=loggedInUser.getName();
User User=userRepository.findByEmailAddress(电子邮件);
ArrayList主题=新建ArrayList();
for(主题子:user.getSubject()){
subjects.add(sub.getSubjectName());
}
model.addAttribute(“主题”,主题);
返回“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]}

@KedarJoshi,这很有效!非常感谢你,你听起来像是一个传奇故事,非常成功,非常感谢!真不敢相信我没试过,星期一一定累了。