問題:我有一個Purchase Order表和Country表,其中的主鍵是我PO表中的外鍵。在前端,我有一個表單(angularJS),用於創建一個新的PO,其中包含一組字段和大約9個下拉列表字段,用戶可以在其中選擇PO所需的任何信息。 爲了舉例,我將使用國家下拉菜單。 (所有下拉菜單都從數據庫中填充)。Spring MVC,Angular AngularJS下拉列表 - 持久化問題ManyToOne
我遇到的問題是持久性。提交表單後,如果使用調試器並檢查傳遞給服務的Purchase Order對象,則除屬於PO表的其他正常輸入字段外,所有的下拉對象都爲空。
我認爲問題是Angular,如果我檢查我的Chrome瀏覽器下的開發人員工具下的網絡選項卡,我看到國家通過countryData:2。事情是我是Angular的新手,所以我在這個階段對於如何處理這個問題感到困惑,因爲數據來自下拉列表以及我應該如何返回國家對象。這裏http://tinypic.com/r/2rd9pxl/8我從瀏覽器中放置了我的網絡選項卡的屏幕截圖,其中顯示了正在發佈的數據。
填充國家下拉列表
<div class="control-group">
<label class="control-label">*Country</label>
<div class="input-append">
<div data-ng-init="getCountryDataFromServer()">
<b>Person Data:</b> <select id="countryData" ng-model="contact.countryData">
<option value="">-- Select Countries --</option>
<option data-ng-repeat="country in countries" value="{{country.countryId}}">{{country.countryname}}</option>
</select><br>
</div>
</div>
<div class="input-append">
<label>
<span class="alert alert-error"
ng-show="displayValidationError && newContactForm.countryData.$error.required">
<spring:message code="required"/>
</span>
</label>
</div>
</div>
型號
@Entity
@Table(name = "PURCHASEORDER",schema = "POTOOL")
@Audited
public class PurchaseOrder {
@Id
@GeneratedValue
private int id;
private String tower;
private Double revenue;
//other properties of the PO
@ManyToOne(fetch=FetchType.EAGER)
@JoinColumn(name = "country_id")
private Country country;
//Other ManyToOne relationships
//Getter and setters
控制器
@RequestMapping(method = RequestMethod.POST, produces = "application/json")
public ResponseEntity<?> create(@ModelAttribute("contact") PurchaseOrder purchaseOrder,
@RequestParam(required = false) String searchFor,
@RequestParam(required = false, defaultValue = DEFAULT_PAGE_DISPLAYED_TO_USER) int page,
Locale locale) {
purchaseOrderServiceImpl.save(purchaseOrder);
if (isSearchActivated(searchFor)) {
return search(searchFor, page, locale, "message.create.success");
}
return createListAllResponse(page, locale, "message.create.success");
}
服務
public void save(PurchaseOrder purchaseOrder) {
purchaseOrderRepository.save(purchaseOrder);
}
庫
public interface PurchaseOrderRepository extends
PagingAndSortingRepository<PurchaseOrder, Integer> {
}
DTO是解決方案。感謝您指出了這一點。我在我的控制器中添加了一個DTO,並從那裏獲得了魔法...... – user2342259 2015-03-04 11:32:44