0

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. 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).

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?)

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:

    @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]
  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. before I even click on the Delete button.
5
  • What is this list ? result of search in DB ? where is any method that will allow to identify object by ID field ? Commented Jan 2, 2015 at 15:43
  • the list is not from DB, it will be a temporary list, after using it for updating my Device table in DB, I will not need it anymore, Commented Jan 2, 2015 at 16:04
  • so you need to introduce some identifier to it - even simple index of the item within the list, sorry @behzad, but there's to much unknown variables to guess what are you trying to do. You should realize that you need any identifier for each element (and describe it in the question) otherwise we can just make blind shoots. Commented Jan 2, 2015 at 16:08
  • PS: Just remember that Play is stateless between the request, no common points between two list if they are not persisted in any way - at least in-mem cache Commented Jan 2, 2015 at 16:10
  • thanks, I will add an identifier, if things did not get correct, will write all the details :-) Commented Jan 2, 2015 at 16:11

1 Answer 1

1

That's simple:

Iterate list with @for statement wrapping each element with separate form:

@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).

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.