title: How to map a complex structure in jsp view to a model object in spring MVC
body: <p>I'm using spring mvc for the first time and I'm trying to display and edit a structure in jsp.</p>
  
  <p>I have a class Snippet, which holds a list of objects of type Sentence:</p>
  
  <pre><code>public class Snippet {
    private int id;
    private List&lt;Sentence&gt; sentences;
    // getters, setters, default constructor
  }
  
  public class Sentence {
    private int id;
    private int scale;
    private String text;
    // getters, setters, default constructor
  }
  </code></pre>
  
  <p>In my controller I give a new snippet for editing and when user clicks "save" store it to my db, then return another one. Currently the sentences list of the snippet is null:</p>
  
  <pre><code>@RequestMapping("/snippet")
  public ModelAndView getSnippet() {
    return new ModelAndView("snippet", "snippet", snippetService.getSnippet());
  }
  
  @RequestMapping("/save")
  public ModelAndView saveSnippet(@ModelAttribute Snippet snippet) {
    if(snippet != null &amp;&amp; snippet.getSentences() != null &amp;&amp; !snippet.getSentences().isEmpty()) {
      snippetService.updateSnippet(snippet);
    }
    return new ModelAndView("snippet", "snippet", snippetService.getSnippet());
  }
  </code></pre>
  
  <p>In my snippet.jsp I'd like to display the snippet sentences with their scale, and on save, pass snippet with sentences and scales to the controller for storage:</p>
  
  <pre><code>&lt;form:form method="post" action="save" modelAttribute="snippet"&gt;
    ...
    &lt;c:forEach var="sentence" items="${snippet.sentences}"&gt;
      &lt;tr&gt;
        &lt;td&gt;${sentence.id}&lt;/td&gt;
        &lt;td&gt;${sentence.text}&lt;/td&gt;
        &lt;td&gt;&lt;input type="range" name="sentence.scale" value="${sentence.scale}"
           path="sentence.scale" min="0" max="5" /&gt;&lt;/td&gt;
      &lt;/tr&gt;
    &lt;/c:forEach&gt;
    &lt;tr&gt;
      &lt;td colspan="4"&gt;&lt;input type="submit" value="Save" /&gt;&lt;/td&gt;
    &lt;/tr&gt;
  </code></pre>
  
  <p>I think I have to find the right way to use the path attribute but I can't figure it out.</p>