gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 04:09:05 24 4
gpt4 key购买 nike

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

膳食.java

@Entity
public class Meal {

@Id
@GeneratedValue
private Long id;

@OneToOne
private Student mealCook;

private String mealName;

private int mealPrice;

学生.java

@Entity
public class Student {

@Id
@GeneratedValue
private Long id;

private String studentName;

膳食 Controller .java

@Controller
@RequestMapping("/m")
public class MealController {

private final MealRepository mealRepository;
private final StudentRepository studentRepository;

public MealController(MealRepository mealRepository, StudentRepository studentRepository){
this.mealRepository = mealRepository;
this.studentRepository = studentRepository;
}

@GetMapping
public ModelAndView list(){
Iterable<Meal> meals = this.mealRepository.findAll();
return new ModelAndView("meals/list" , "meals", meals);
}

@GetMapping("{id}")
public ModelAndView view(@PathVariable("id") Meal meal) {
return new ModelAndView("meals/view", "meal", meal);
}

@GetMapping(params = "form")
public String createForm(@ModelAttribute Meal meal) {
return "meals/form";
}

@PostMapping
public ModelAndView create(@Valid Meal meal, BindingResult result,
RedirectAttributes redirect) {
Iterable<Student> students = this.studentRepository.findAll();
if (result.hasErrors()) {
return new ModelAndView("meals/form", "formErrors", result.getAllErrors());
}
meal = this.mealRepository.save(meal);
redirect.addFlashAttribute("globalMessage", "view.success");
return new ModelAndView("redirect:/m/{meal.id}", "meal.id", meal.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>

现在的目标是通过从表单的下拉菜单中选择 studentName 来将 1 名学生添加到一顿饭中。

但我正在努力研究如何将学生列表从 Controller 传递到 View 并将其显示在下拉列表中。

最佳答案

您应该在打开表单的任何地方的 Controller 中添加学生名单:

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

<option> :

<option th:each="item : ${studentList}"
th:value=${item.id} // value you want...
th:text=${item.name}>
</option>

和改变实体:

Meal.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;

Student.java:

@Entity
public class Student {

@Id
@GeneratedValue
private Long id;

private String studentName;

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

关于java - 使用 Spring-mvc 和 thymeleaf 从实体填充下拉列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56201772/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com