Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/386.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 使用Spring mvc和thymeleaf从实体填充下拉列表_Java_Spring Mvc_Thymeleaf - Fatal编程技术网

Java 使用Spring mvc和thymeleaf从实体填充下拉列表

Java 使用Spring mvc和thymeleaf从实体填充下拉列表,java,spring-mvc,thymeleaf,Java,Spring Mvc,Thymeleaf,我正在开发一个应用程序,其中我想使用表单中的下拉列表将一名学生添加到一顿饭中。我的代码如下所示: java Student.java MealController.java @控制器 @请求映射(“/m”) 公共类微控制器{ 私人最终计量储存; 私人期末考试预备课程预备课程预备课程; 公共计量控制员(计量储存计量储存、研究储存、研究储存){ this.mealRepository=mealRepository; this.studentRepository=studentRepository;

我正在开发一个应用程序,其中我想使用表单中的下拉列表将一名学生添加到一顿饭中。我的代码如下所示:

java Student.java MealController.java
@控制器
@请求映射(“/m”)
公共类微控制器{
私人最终计量储存;
私人期末考试预备课程预备课程预备课程;
公共计量控制员(计量储存计量储存、研究储存、研究储存){
this.mealRepository=mealRepository;
this.studentRepository=studentRepository;
}
@GetMapping
公共模型和视图列表(){
Iterable Founds=this.mealRepository.findAll();
返回新模型和视图(“膳食/列表”、“膳食”、“膳食”);
}
@GetMapping(“{id}”)
公共模型和视图(@PathVariable(“id”)膳食){
返回新模型和视图(“膳食/视图”、“膳食”、“膳食”);
}
@GetMapping(params=“form”)
公共字符串createForm(@ModelAttribute){
返回“膳食/表格”;
}
@邮戳
公共模型和视图创建(@Valid-fine,BindingResult,
重定向属性(重定向){
Iterable studentrespository.findAll();
if(result.hasErrors()){
返回新的ModelAndView(“膳食/表单”,“表单错误”,result.getAllErrors());
}
膳食=此.mealRepository.save(膳食);
redirect.addFlashAttribute(“globalMessage”、“view.success”);
返回新的ModelAndView(“重定向:/m/{mean.id}”、“mean.id”、mean.getId());
}
最后是我的视图->form.html

<form id="mealForm" th:action="@{/m/(form)}" th:object="${meal}" action="#" method="post">

    <div class="form-group">
        <label for="mealName">Meal Name</label>
        <input type="text" th:field="*{mealName}" th:class="${'form-control' + (#fields.hasErrors('mealName') ? ' is-invalid' : '')}">
    </div>

    <div class="form-group">
        <label for="mealCook">Meal Cook</label>
        <select th:field="*{mealCook}">
            <option th:each= ??
                    th:value= ??
                    th:text= ??</option>
        </select>
    </div>

    <button type="submit" class="btn btn-primary">Submit</button>
</form>

餐名
厨师
在打开表单的任何位置,都应在controller中添加学生列表:

    ModelAndView model = new ModelAndView("studentList");
    model.addObject(students) // get list from database or...
    // students =studentRepository.findAll() or use your exclusive query

Student.java

@Entity
    public class Meal {

@Id
@GeneratedValue
  private Long id;

  @OneToOne
  @JoinColumn(name = "meel_cook_id")
  private Student mealCook;

  private String mealName;

  private int mealPrice;
@Entity
 public class Student {

@Id
@GeneratedValue
private Long id;

private String studentName;

@OneToOne(mappedBy = "mealCook")
@JsonBackReference
private Meal meal;

将学员列表作为属性传递给控制器中的模型。 e、 g

然后可以使用命令读取该属性

<option th:each="student:${students}"
                    th:value="${student.studentName}"
                    th:text="${student.studentName}">

</option>


谢谢你的回答!我已经成功地在下拉菜单中显示了学生,但是学生(mealCook)将不会显示在我的列表概述中。你知道我如何解决此问题吗?我添加了两张图片以使其更清晰。请检查数据库并确保数据是存储的。除学生的姓名外,其他内容都存储得很好。出于某种原因,它存储为“用餐\烹饪\ id:NULL”。
<option th:each="item : ${studentList}"
        th:value=${item.id} // value you want...
        th:text=${item.name}>
</option>
@Entity
    public class Meal {

@Id
@GeneratedValue
  private Long id;

  @OneToOne
  @JoinColumn(name = "meel_cook_id")
  private Student mealCook;

  private String mealName;

  private int mealPrice;
@Entity
 public class Student {

@Id
@GeneratedValue
private Long id;

private String studentName;

@OneToOne(mappedBy = "mealCook")
@JsonBackReference
private Meal meal;
@GetMapping(params = "form")
public String createForm(@ModelAttribute Meal meal, Model theModel) {
    theModel.addAttribute("students", studentRepository.findAll());

    return "meals/form";
}
<option th:each="student:${students}"
                    th:value="${student.studentName}"
                    th:text="${student.studentName}">

</option>