我对Java Spring MVC还是比较陌生,我正在开发一个列出香波及其成分的Web应用程序。我想我可能误会了一些东西。如果您有任何问题让我知道,我将继续尝试解释情况。
我当前面临的问题是弄清楚如何控制用户输入到表单中的信息。特别是下拉菜单(多项选择)。
成分数据库已经有1000多个条目,并且很可能很快将不再需要。用户将能够添加新类型的洗发水,但将无法添加新成分。我已经有一张桌子,里面装满了不同的食材。问题是,当我在表单上选择属于洗发水的成分并点击提交时,它将再次将该材料插入表格中。 This is an img of the actual Table entries, As you can see, it is adding entries like 8,33,62,18.. 而是将其添加到Shampoo_Ingredients表中。 This is a img of Shampoo_Ingredient. As you can see, It is using the key of the newly inserted Ingredient instead of selecting the ingredient chosen by the user directly.
JSP文件
<form:form cssClass="form" action="saveShampoo" modelAttribute="shampoo" method="POST">
<table>
<tbody>
<tr>
<td><label>Name:</label></td>
<td><form:input path="name" /></td>
</tr>
<tr>
<td><label>Company:</label></td>
<td><form:input path="company" /></td>
</tr>
<tr>
<td><label>Ingredients:</label></td>
<td><form:select cssClass="test" multiple="true" path="ingredients" >
<form:options items="${ingredients}" itemLabel="name" itemValue="id" />
<td>${ingredient.name}</td>
</form:select></td>
</tr>
</tbody>
</table>
<input id= "submit" type="submit" value="Save"/>
</form:form
控制器文件
@GetMapping("/showFormForAdd")
public String showFormForAdd(Model theModel, ModelMap modelMap) {
//create model attribute to bind form data
Shampoo theShampoo = new Shampoo();
modelMap.addAttribute("shampoo", theShampoo);
List<Ingredient> theIngredients = ingredientDAO.getIngredients();
theModel.addAttribute("ingredients", theIngredients);
return "shampoo-form";
}
@PostMapping("/saveShampoo")
public String saveShampoo(@ModelAttribute("shampoo") Shampoo theShampoo) {
System.out.println(theShampoo.getIngredients());
shampooDAO.saveShampoo(theShampoo);
return "redirect:/shampoo/list";
}
}
实体文件
@Entity
@Table(name="shampoo")
public class Shampoo {
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY )
@Column(name="shampoo_id")
private int id;
@Column(name="shampoo_name")
private String name;
@OneToOne(cascade=CascadeType.ALL)
@JoinColumn(name="shampoo_company")
private Company company;
@ManyToMany(fetch = FetchType.LAZY, cascade = CascadeType.ALL)
@JoinTable(
name="shampoo_ingredients",
joinColumns=@JoinColumn(name="fk_shampoo"),
inverseJoinColumns=@JoinColumn(name="fk_ingredient")
)
private List<Ingredient> ingredients;
public Shampoo() {
}
public Shampoo(String name) {
this.name = name;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Company getCompany() {
return company;
}
public void setCompany(Company company) {
this.company = company;
}
public List<Ingredient> getIngredients() {
return ingredients;
}
public void setIngredients(List<Ingredient> ingredients) {
this.ingredients = ingredients;
}
@Override
public String toString() {
return "Shampoo [id=" + id + ", name=" + name + ", company=" + company + "]";
}
洗发水DAO
@Repository
public class ShampooDAOImpl implements ShampooDAO {
@Autowired
private SessionFactory sessionFactory;
@Transactional
public List<Shampoo> getShampoos() {
//get current hibernate session
Session currentSession = sessionFactory.getCurrentSession();
//create query
Query<Shampoo> theQuery = currentSession.createQuery("from Shampoo", Shampoo.class);
//execute query and get result list
List<Shampoo> shampoos = theQuery.getResultList();
//return list of shampoo
return shampoos;
}
@Transactional
public void saveShampoo(Shampoo theShampoo) {
Session currentSession = sessionFactory.getCurrentSession();
currentSession.save(theShampoo);
}
}
成分实体
@Entity
@Table(name="ingredient")
public class Ingredient {
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
@Column(name="ingredient_id")
private int id;
@Column(name="ingredient_name")
private String name;
@ManyToMany(fetch = FetchType.LAZY, cascade = CascadeType.ALL)
@JoinTable(
name="shampoo_ingredients",
joinColumns=@JoinColumn(name="fk_ingredient"),
inverseJoinColumns=@JoinColumn(name="fk_shampoo")
)
private List<Shampoo> Shampoo;
public Ingredient() {
}
public Ingredient(String name) {
this.name = name;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public List<Shampoo> getShampoo() {
return Shampoo;
}
public void setShampoo(List<Shampoo> shampoo) {
Shampoo = shampoo;
}
@Override
public String toString() {
return "Ingredient [id=" + id + ", name=" + name + "]";
}