Spring MVC,Angular AngularJS 下拉列表 - 关于持久化 ManyToOne 的问题

Spring MVC, Angular AngularJS drop-down list - problems on persist ManyToOne

问题: 我有一个采购订单 table 和国家 table,其中主键是我的 PO table 中的一个外键。在前端,我有一个表单 (angularJS) 用于创建一个带有一堆字段的新采购订单和大约 9 个下拉列表字段表单,用户可以在其中 select 需要的任何信息宝。 为了示例的缘故,我将使用“国家/地区”下拉列表。 (所有下拉菜单都从数据库中填充)。

我的问题是持久性。提交表单后,如果我使用调试器并检查传递给服务的 Purchase Order 对象,我的所有下拉对象都是空的,除了属于 PO table 的其他正常输入字段。

我认为问题出在 Angular,如果我在我的 chrome 浏览器中的开发者工具下检查网络选项卡,我会看到传递的国家/地区为 countryData:2。事情是我是 Angular 的新手,所以在这个阶段我有点困惑我应该如何处理这个数据来自下拉列表以及我应该如何 return 国家对象。在这里 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> {


}

我认为 angular 没有任何问题,因为您已经为下拉列表定义了 ng-model="contact.countryData" 并且选项具有值来自 value="{{country.countryId}}"。当您提交值时 country.countryId 将发送到服务器,这绝对没问题。我认为你应该有转换机制,将数据从 UI 转换为 Hibernate Entity(即 PurchaseOrder)。通常 DTO(数据传输对象)用于从 UI 获取数据并使用 setter 和 getter 我们设置为 Hibernate 实体,稍后您将保存该实体。 这个问题可能对你有帮助。

DTO to Entity And Entity to DTO

当您使用 HTML(而不是 JSP)时,最好使用 @RequestBody 代替 @ModelAttribute 以获得完整的数据主体。请参考以下链接: Link 1 Link 2