I am using spring mvc for the first time and I am trying to display and edit the structure in jsp.
I have a Snippet class that contains a list of objects of type Sentence:
public class Snippet {
private int id;
private List<Sentence> sentences;
}
public class Sentence {
private int id;
private int scale;
private String text;
}
In my controller, I give a new snippet for editing, and when the user clicks "Save", save it in my db, and then return another one. Currently, the list of sentences for the fragment is null:
@RequestMapping("/snippet")
public ModelAndView getSnippet() {
return new ModelAndView("snippet", "snippet", snippetService.getSnippet());
}
@RequestMapping("/save")
public ModelAndView saveSnippet(@ModelAttribute Snippet snippet) {
if(snippet != null && snippet.getSentences() != null && !snippet.getSentences().isEmpty()) {
snippetService.updateSnippet(snippet);
}
return new ModelAndView("snippet", "snippet", snippetService.getSnippet());
}
In my snippet.jsp, I would like to display fragment sentences with their scale and save, transfer the fragment with sentences and scales for the controller for storage:
<form:form method="post" action="save" modelAttribute="snippet">
...
<c:forEach var="sentence" items="${snippet.sentences}">
<tr>
<td>${sentence.id}</td>
<td>${sentence.text}</td>
<td><input type="range" name="sentence.scale" value="${sentence.scale}"
path="sentence.scale" min="0" max="5" /></td>
</tr>
</c:forEach>
<tr>
<td colspan="4"><input type="submit" value="Save" /></td>
</tr>
I think I need to find the correct way to use the path attribute, but I cannot figure it out.