【发布时间】:2015-05-04 00:45:45
【问题描述】:
问题:我有一个 Purchase Order 表和 Country 表,其中主键是 PO 表中的外键。在前端,我有一个表单(angularJS),用于创建一个带有一堆字段和大约 9 个下拉列表字段表单的新 PO,用户可以在其中选择 PO 所需的任何信息。 为了示例,我将使用 Country 下拉菜单。 (所有下拉菜单都从数据库中填充)。
我的问题在于坚持。提交表单后,如果我使用调试器并检查正在传递给服务的采购订单对象,我的所有下拉对象都是空的,但属于 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> {
}
【问题讨论】:
标签: angularjs spring hibernate model-view-controller