我正在使用Spring创建一个简单的Web项目。我的数据库中有两个表:Recipe和Category。每个配方都分配到一个类别。我有一个页面,其中有我的类别列表,单击其中的一个类别后,我将转到包含该类别的收件人列表的页面。在列表下,我有一个按钮可将新配方添加到类别。我的问题是我无法为表单中的配方对象分配category_id。现在,我将具有category_id = null的配方发送给控制器。调试代码时,我在方法 addForm() 中获得了适当的类别。
这是我的代码:
班级食谱
@Entity
public class Recipe {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@Column (unique = true)
private String name;
private int time;
@ManyToOne
private Category category;
@Column(length = 1024)
private String ingredients;
@Column(length = 1024)
private String instructions;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getTime() {
return time;
}
public void setTime(int time) {
this.time = time;
}
public String getIngredients() {
return ingredients;
}
public void setIngredients(String ingredients) {
this.ingredients = ingredients;
}
public String getInstructions() {
return instructions;
}
public void setInstructions(String instructions) {
this.instructions = instructions;
}
public Category getCategory() {
return category;
}
public void setCategory(Category category) {
this.category = category;
}
public Recipe(String name, int time, Category category, String ingredients, String instructions) {
this.name = name;
this.time = time;
this.category = category;
this.ingredients = ingredients;
this.instructions = instructions;
}
public Recipe() {
}
}
课程类别:
@Entity
public class Category {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@Column(unique = true)
private String name;
@OneToMany
@JoinColumn(name = "category_id")
private List <Recipe> recipes;
public List<Recipe> getRecipes() {
return recipes;
}
public void setRecipes(List<Recipe> recipes) {
this.recipes = recipes;
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Category(String name) {
this.name = name;
}
public Category() {
}
}
Controller类中的代码:
@RequestMapping("/add")
public String addForm(Category category, Model model)
{
//this category has a proper name and id
model.addAttribute("category", category);
model.addAttribute("recipe", new Recipe() );
return "addRecipe";
}
@RequestMapping("/save")
public String addRecipe( @ModelAttribute Recipe recipe )
{
//here I have a recipe with category_id=null
recipeRepository.save(recipe);
return "redirect:/";
}
和html文件
<form th:action = "@{/save}" th:object = "${recipe}" method="post">
<input type = "text" th:field = "*{name}" placeholder = "Name"><br>
<input type = "number" th:field = "*{time}" placeholder = "Time"><br>
<input type = "hidden" th:field = "*{category}" th:value = "${category.id}"><br>
<textarea rows = "10" th:field = "*{ingredients}" placeholder="Ingredients"></textarea><br>
<textarea rows = "10" th:field = "*{instructions}" placeholder="Instructions"></textarea><br>
<button type="submit">ADD</button>
非常感谢您的帮助。