[英]Passing a List of objects from view to Controller in play framework
In my play(FOR JAVA) app, I have a list of objects (java.util.List) that will be passed to a view and will be shown to the user. 在我的play(FOR JAVA)应用程序中,我有一个对象列表(java.util.List),该对象列表将传递给视图并显示给用户。 Then user can may or may not delete some of the objects in the list, and after that, I want to pass the edited list back to the Controller BUT I can't do the last part(passing from view to controller).
然后,用户可以删除列表中的某些对象,也可以不删除,然后,我想将编辑后的列表传递回Controller,但是我不能做最后一部分(从视图到控制器的传递)。
Because my list might be big, I don't want to do that with a GET (GET is also kind of unsafe?!) and don't know how to do it with a POST, (or is there any other solution?) 因为我的清单可能很大,所以我不想用GET来做(GET也很不安全?!),也不知道如何用POST来做(或者还有其他解决方案吗?)
So it would be great if I could get some help with this. 因此,如果我能对此有所帮助,那就太好了。
The objects inside my list are from this type: 我列表中的对象就是这种类型的:
public class CalObj {
private String pdfFileName;
private String serialNo;
private Date calDate;
private Device device;
}
UPDATE: thanks @biesior , my View(calExtractionResults.scala.html) now looks like this: 更新:谢谢@biesior,我的View(calExtractionResults.scala.html)现在看起来像这样:
@for(calObj <- calObjList) {
<tr>
<td> @calObj.getPdfFileName</td>
<td> @calObj.getSerialNo</td>
<td> @calObj.getDevice.name</td>
<td> @calObj.getDevice.calDateToString()</td>
<td> @calObj.getCalDate</td>
<td>
<form action="@DateExtractorContr.updateList(calObjList, calObj)" method="POST">
<input type="hidden" name="serialNo" value="@calObj.getSerialNo"/>
<input type="submit" value="Delete"/>
</form>
</td>
</tr>
}
and this is in my Controller: 这是在我的控制器中:
public static Result updateList(List<CalObj> calObjs, CalObj objToDel){
List<CalObj> newList = calObjs;
newList.remove(objToDel);
return ok(calExtractionResults.render(newList));
}
but when I open the related page, there are problems: 但是当我打开相关页面时,出现了问题:
[ConcurrentModificationException: null]
[ConcurrentModificationException: null]
That's simple: 很简单:
Iterate list with @for
statement wrapping each element with separate form: 使用
@for
语句迭代列表,用单独的形式包装每个元素:
@for(item <- yourList) {
<h1>@item.name</h1>
<form action="/link/to/delete/action" method="POST">
<input type="hidden" name="id" value="@item.id"/>
<input type="submit" value="Delete"/>
</form>
}
So after deleting the item you can redirect to the list view again. 因此,删除项目后,您可以再次重定向到列表视图。
As you can see you need some unique ID (maybe serialNo
keeps the role in your case, dunno). 如您所见,您需要一些唯一的ID(也许
serialNo
在您的情况下保留角色,dunno)。
Edit: Of course you can also create one form witch checkboxes as array, to send it at once if you want to delete many elements. 编辑:当然,您也可以创建一种形式的女巫复选框作为数组,如果要删除许多元素,可以立即发送。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.