首页 > 解决方案 > 在 Play 中保存相关对象的列表!来自表单的框架 1.x

问题描述

我在 Play 模型中有一个 Offer 对象,它可以有一个或多个 OfferLines,所以我有

@Required
@ManyToOne
public Offer offer;

在优惠线上

@OneToMany(mappedBy="offer", cascade= CascadeType.ALL)
public List<OfferLine> offerLines;

正在促销。

当我打开我的表单时,我有以下内容来填充要渲染的东西

Offer myOffer = new Offer();
myOffer.offerFrom = currentUser;
myOffer.targetSwag = targetSwag;
myOffer.offerTo = targetSwag.Owner;
myOffer.offerLines = new ArrayList<OfferLine>();

for (Swag swagline : mySwag)
{
    OfferLine offerLine = new OfferLine();
    offerLine.quantity=0;
    offerLine.swag = swagline;
    myOffer.offerLines.add(offerLine);
}

myOffer.save();
renderArgs.put("myOffer",myOffer);
render();

最后,我的表格看起来像这样(尽管有缩进......)

#{form @submitOffer(), id:'submitOffer'}
<table border="1">
    <thead>
    <tr>
        <th width="30" align="left">ID</th>
        <th width="80" align="left">Swag</th>
        <th width="30" align="left">Quantity</th>
    </tr>
    </thead>
    <tbody>
    #{list items:myOffer.offerLines, as:'offerline'}

    <tr>
    <td height="32" width="30"><input type="text" id="offerline.id" 
name="offerline.id" value="${offerline.id}"/></td>
    <td height="32" width="80">
        ${offerline.swag.Name}
    </td>
    <td height="32">
        <input class="field" id="offerline.quantity" name="offerline.quantity" type="number" min="0" max="${offerline.swag.Quantity}" value="${offerline.quantity}"/>
    </td>
</tr>
    #{/list}
    </tbody>
</table>

当我提交时,我调用这样的方法......

public static void submitOffer(Integer amountRequested, Offer myOffer)
{
    myOffer.save();
    System.out.println("Requested " + amountRequested);
    System.out.println("My offer coming out " + myOffer.toString());


    renderJSON(myOffer);

}

但是我的 Offerlines 不在 JSON 中,即它们似乎没有在 Offer 对象中传递回。如何在一个屏幕中编辑和保存与报价相关的报价行列表?

[更新] 我测试了传递给表单的 MyOffer 对象,它有报价线,它只是不再回来了..

    {
  "offerLines": [
    {
      "swag": {
        "Name": "Test Swag",
        "Description": "Big ol' test swag",
        "Quantity": 10,
        "Owner": {
          "username": "robcowell",
          "password": "REDACTED",
          "email": "REDACTED",
          "fullname": "Rob Cowell",
          "isAdmin": true,
          "isActive": true,
          "Photo": {
            "bucket": "REDACTED",
            "key": "d5654f8f-0cd0-45a2-a77f-3edda01feca0",
            "contentLength": 0
          },
          "hasChangedPassword": true,
          "id": 1
        },
        "Photo": {
          "bucket": "REDACTED",
          "key": "b0774667-a32a-4508-81ba-cdb84ebc689f",
          "contentLength": 0
        },
        "id": 52
      },
      "quantity": 0,
      "id": 134
    },
    {
      "swag": {
        "Name": "Swag 2",
        "Description": "Swag 2",
        "Quantity": 5,
        "Owner": {
          "username": "robcowell",
          "password": "REDACTED",
          "email": "REDACTED",
          "fullname": "Rob Cowell",
          "isAdmin": true,
          "isActive": true,
          "Photo": {
            "bucket": "REDACTED",
            "key": "d5654f8f-0cd0-45a2-a77f-3edda01feca0",
            "contentLength": 0
          },
          "hasChangedPassword": true,
          "id": 1
        },
        "id": 54
      },
      "quantity": 0,
      "id": 135
    },
    {
      "swag": {
        "Name": "Swag 3",
        "Description": "Mo' Swag",
        "Quantity": 1,
        "Owner": {
          "username": "robcowell",
          "password": "REDACTED",
          "email": "REDACTED",
          "fullname": "Rob Cowell",
          "isAdmin": true,
          "isActive": true,
          "Photo": {
            "bucket": "REDACTED",
            "key": "d5654f8f-0cd0-45a2-a77f-3edda01feca0",
            "contentLength": 0
          },
          "hasChangedPassword": true,
          "id": 1
        },
        "id": 55
      },
      "quantity": 0,
      "id": 136
    },
    {
      "swag": {
        "Name": "Test swag with an inordinately long name purely for the purposes of testing the rendering of these columns",
        "Description": "As above",
        "Quantity": 1,
        "Owner": {
          "username": "robcowell",
          "password": "REDACTED",
          "email": "REDACTED",
          "fullname": "Rob Cowell",
          "isAdmin": true,
          "isActive": true,
          "Photo": {
            "bucket": "REDACTED",
            "key": "d5654f8f-0cd0-45a2-a77f-3edda01feca0",
            "contentLength": 0
          },
          "hasChangedPassword": true,
          "id": 1
        },
        "id": 60
      },
      "quantity": 0,
      "id": 137
    }
  ],
  "offerTo": {
    "username": "todd",
    "password": "REDACTED",
    "email": "REDACTED",
    "fullname": "Todd Halfpenny",
    "isAdmin": false,
    "isActive": true,
    "hasChangedPassword": true,
    "id": 20
  },
  "offerFrom": {
    "username": "robcowell",
    "password": "REDACTED",
    "email": "REDACTED",
    "fullname": "Rob Cowell",
    "isAdmin": true,
    "isActive": true,
    "Photo": {
      "bucket": "REDACTED",
      "key": "d5654f8f-0cd0-45a2-a77f-3edda01feca0",
      "contentLength": 0
    },
    "hasChangedPassword": true,
    "id": 1
  },
  "targetSwag": {
    "Name": "Todd's tshirts",
    "Description": "Nice tshirts by Todd",
    "Quantity": 10,
    "Owner": {
      "username": "todd",
      "password": "REDACTED",
      "email": "REDACTED",
      "fullname": "Todd",
      "isAdmin": false,
      "isActive": true,
      "hasChangedPassword": true,
      "id": 20
    },
    "Photo": {
      "bucket": "REDACTED",
      "key": "f627cbcf-7eba-400d-a1f9-dcf1ab457709",
      "contentLength": 0
    },
    "id": 53
  },
  "id": 133
}

标签: jpaplayframework-1.x

解决方案


您可以尝试以下方法来设置索引以处理数组。

#{list items:myOffer.offerLines, as:'offerline'}

<tr>
<td height="32" width="30"><input type="text" id="offerline.id" name="offerline[${offerline_index-1}].id" value="${offerline.id}"/></td>
<td height="32" width="80">
    ${offerline.swag.Name}
</td>
<td height="32">
    <input class="field" id="offerline.quantity" name="offerline[${offerline_index-1}].quantity" type="number" min="0" max="${offerline.swag.Quantity}" value="${offerline.quantity}"/>
</td>
</tr> 
#{/list}

推荐阅读