【发布时间】:2018-12-15 20:27:04
【问题描述】:
我在 Play 模型中有一个 Offer 对象,它可以有一个或多个 OfferLines,所以我有
@Required
@ManyToOne
public Offer offer;
在OfferLine上
和
@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
}
【问题讨论】:
-
在进入表单的途中测试了对象JSON,并且Offerlines存在..
-
我没有测试过,但我的猜测是在返回 submitOffer 的路上,没有
myoffer.id可以链接回您提交的offerlines。也许添加一个带有报价 id 的隐藏字段。您在打印行中看到了什么? -
是的,传递了 myOffer.Id,但 println 给出了前后的差异 -
Offerlines passed to form - [149(Test Swag - 0), 150(Swag 2 - 0), 151(Swag 3 - 0), 152(Test swag with an inordinately long name purely for the purposes of testing the rendering of these columns - 0)] Download for swag 1 Swag photo utils.S3Blob@faec98 Requested 1 My offer coming out 148 Todd's tshirts Offerlines [] -
所以在 submitOffer 方法上,myOffer 的“父”属性似乎已设置,但我的 offerLines 集合被清除了
-
如果您查看playframework.com/documentation/1.5.x/controllers#binding,它建议使用 POJO 数组,它需要 http 数组表示法。
标签: jpa playframework-1.x