Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to submit a list of Objects in Play Framework
    primarykey
    data
    text
    <p>Here my test setup: </p> <p><strong>The Model:</strong></p> <pre><code>package models; import javax.persistence.Entity; import play.db.jpa.Model; @Entity public class Item extends Model { public String propertyA; public String propertyB; public String getFullName() { return this.propertyA + " - " + this.propertyB; } } </code></pre> <p><strong>The Controller:</strong></p> <pre><code>package controllers; import play.*; import play.mvc.*; import java.util.*; import models.*; public class Application extends Controller { public static void index() { List&lt;Item&gt; allItems = Item.findAll(); render(allItems); } public static void save(List&lt;Item&gt; items) { if (items == null) { Logger.info("no items"); } else { for (Item item : items) { Logger.info("item: %s",item); } Logger.info("-------"); } index(); } } </code></pre> <p><strong>The view:</strong></p> <pre><code>#{extends 'main.html' /} #{set title:'Home' /} #{form @Application.save()} Select Items: &lt;div&gt; &lt;select name="items" style="width: 200px;" multiple="multiple" size="10"&gt; #{list items:allItems, as: 'item'} &lt;option value=${item.id} &gt;${item.fullName}&lt;/option&gt; #{/list} &lt;/select&gt; &lt;/div&gt; &lt;input type="submit" value="Save" /&gt; #{/form} </code></pre> <p>When I select two Items in the Select box, I get the following Log output:</p> <pre><code>06:22:20,560 INFO ~ ------- 06:22:26,991 INFO ~ item: null 06:22:26,991 INFO ~ item: null </code></pre> <p>It looks like play maps the selection to a List of <code>null</code> objects. Even when I change the view to:</p> <pre><code>&lt;option value=${item} &gt;${item.fullName}&lt;/option&gt; </code></pre> <p>I get <code>null</code> values.</p> <p>But when I change the controller to:</p> <pre><code>public static void save(List&lt;Long&gt; items) { ... } </code></pre> <p>I at least get a List of correct Ids.</p> <p>How can I change this, so that play creates the correct mapping for my items. I prefer to get <code>List&lt;Item&gt;</code> instead of <code>List&lt;Long&gt;</code> (here I have to perform queries to geht the List of Items).</p> <p>BR, Rene</p> <p><strong>Update:</strong></p> <p>If I run:</p> <pre><code>curl -d "items[0].id=3&amp;items[0].propertyA=bla&amp;items[0].propertyB=blo&amp;items[1].id=4" http://localhost:9000/application/save </code></pre> <p>Wich does a POST with the above parameters, I get:</p> <pre><code>A02:39:53,072 INFO ~ item: bla - blo 02:39:53,072 INFO ~ item: null - null 02:39:53,072 INFO ~ ------- </code></pre> <p>Like this it works, but I do not see how I could change the view, so that it posts the above parameters :-(</p> <p><strong>Update 2:</strong></p> <p>With the help from Felipe I got it to work. This Controller Method has to be added:</p> <pre><code>@Before(only = {"save"}) static void parseParams() { String[] items = params.getAll("items"); params.remove("items"); for (int i = 0; i &lt; items.length; i++) { Item item = Item.findById(Long.parseLong(items[i])); params.put("items[" + i + "].id", items[i]); params.put("items[" + i + "].propertyA", item.propertyA); params.put("items[" + i + "].propertyB", item.propertyB); } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload