简体   繁体   English

在Play框架中将对象列表从视图传递到Controller

[英]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: 但是当我打开相关页面时,出现了问题:

  1. with the code above, I get: [ConcurrentModificationException: null] 使用上面的代码,我得到: [ConcurrentModificationException: null]
  2. if I replace the updateList function with a dummy function that does not make concurrent exception, before showing the page, the program goes through that dummy function. 如果我将updateList函数替换为不会导致并发异常的伪函数,则在显示页面之前,程序将通过该伪函数。 before I even click on the Delete button. 我什至没有点击删除按钮。

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.

相关问题 将参数从视图传递到Play框架上的控制器 - Passing parameter from view to controller on Play Framework 从控制器传递List对象到视图时播放Framework编译错误 - Play Framework compilation error when passing List object from controller to view 在Play框架Java中将视图的复选框列表发送到控制器 - sending a list of checked boxes from view to controller in play framework java 播放框架-渲染对象列表-在视图中找不到 - Play Framework - Render list of Objects - Not Found in the View java播放! 框架:将带有数据表的参数视图传递给控制器​​将返回null - java Play! framework: passing parameter view with datatable to controller returns null 将自定义对象从thymeleaf视图传递到控制器 - Passing Custom Objects from thymeleaf View to a Controller 从控制器显示日期对象/值以在Play Framework / Scala中查看 - Display date object/value from controller to view in Play Framework/Scala 在Play中从视图模板访问缓存的对象! 2.1框架 - Accessing cached objects from view template in Play! 2.1 framework 在游戏框架中保留对象列表 - persisting a list of objects in play framework 如何从播放框架中的配置文件中读取对象列表 - how to read a list of objects from the configuration file in play framework
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM