Spring MVCを初めて使用しています。 Spring、Spring MVC、JPA/Hibernateを使用するアプリケーションを作成しています Spring MVCにモデルオブジェクトへのドロップダウンから値を設定する方法がわかりません。 Customer.javaSpring MVCでのドロップダウン値のバインド
@Entity
public class Customer {
@Id
@GeneratedValue
private Integer id;
private String name;
private String address;
private String phoneNumber;
//Getters and setters
}
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>
Invoice.java
:私はこの非常に一般的なシナリオここで
想像することができ、コードであります
InvoiceCon InvoiceControler.addInvoice()が呼び出されtroller.java
@Controller
public class InvoiceController {
@Autowired
private InvoiceService InvoiceService;
@RequestMapping(value = "/add", method = RequestMethod.POST)
public String addInvoice(@ModelAttribute("invoice") Invoice invoice, BindingResult result) {
invoiceService.addInvoice(invoice);
return "invoiceAdded";
}
}
、請求書インスタンスは、パラメータとして受領。請求書の金額は予想通りですが、顧客インスタンスの属性はNULLです。これは、httpポストが顧客IDを送信し、InvoiceクラスがCustomerオブジェクトを要求するためです。私はそれを変換する標準的な方法は何か分かりません。
Spring Type変換について(http://static.springsource.org/spring/docs/current/spring-framework-reference/html/validation.html)、Controller.initBinder()について読んでいますが、それがこの問題の解決策であるかどうかはわかりません。
アイデア?
私はそれが<フォーム置き換え働かせた:選択パス= "お客様の" .. ./>
–