【问题标题】:Spring MVC, Angular AngularJS drop-down list - problems on persist ManyToOneSpring MVC,Angular AngularJS 下拉列表 - 问题持续存在 ManyToOne
【发布时间】: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


    【解决方案1】:

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

    【讨论】:

    • 我实际上是在使用 JSP,所以@ModelAttribute 更合适
    【解决方案2】:

    我认为没有任何问题,因为您已经为下拉列表定义了 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

    【讨论】:

    • DTO 是解决方案。感谢您指出了这一点。我已经在我的控制器中添加了一个 DTO,并从那里获得了魔法......
    猜你喜欢
    • 2016-04-18
    • 2011-07-21
    • 2020-11-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多