代码之家  ›  专栏  ›  技术社区  ›  Rob Cowell

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

  •  0
  • Rob Cowell  · 技术社区  · 6 年前

    我在游戏模型中有一个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对象中被传递回。如何在一个屏幕中编辑和保存与报价相关的报价行列表?

        {
      "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 回复  |  直到 6 年前
        1
  •  1
  •   Codemwnci    6 年前

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

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