我是Spring MVC的新手.我正在编写一个使用Spring,Spring MVC和JPA/Hibernate的应用程序我不知道如何让Spring MVC设置一个从下拉到模型对象的值.我可以想象这是一个非常常见的场景
这是代码:
Invoice.java
@Entity
public class Invoice{
@Id
@GeneratedValue
private Integer id;
private double amount;
@ManyToOne(targetEntity=Customer.class, fetch=FetchType.EAGER)
private Customer customer;
//Getters and setters
}
Run Code Online (Sandbox Code Playgroud)
Customer.java
@Entity
public class Customer {
@Id
@GeneratedValue
private Integer id;
private String name;
private String address;
private String phoneNumber;
//Getters and setters
}
Run Code Online (Sandbox Code Playgroud)
invoice.jsp
<form:form method="post" action="add" commandName="invoice">
<form:label path="amount">amount</form:label>
<form:input path="amount" />
<form:label path="customer">Customer</form:label>
<form:select path="customer" items="${customers}" required="true" itemLabel="name" itemValue="id"/>
<input type="submit" value="Add Invoice"/>
</form:form>
Run Code Online (Sandbox Code Playgroud)
InvoiceController.java
@Controller
public class InvoiceController { …Run Code Online (Sandbox Code Playgroud) spring-mvc ×1