在Play中保存相关对象的列表!表单中的Framework 1.x

时间:2018-12-15 20:27:05

标签: jpa playframework-1.x

我在Play模型中有一个Offer对象,该对象可以包含一个或多个OfferLine,所以我有

@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
}

1 个答案:

答案 0 :(得分:1)

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

#{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}