【发布时间】:2014-01-16 09:45:58
【问题描述】:
我是第一次使用spring mvc,我正在尝试在jsp中显示和编辑一个结构。
我有一个类 Snippet,它包含一个 Sentence 类型的对象列表:
public class Snippet {
private int id;
private List<Sentence> sentences;
// getters, setters, default constructor
}
public class Sentence {
private int id;
private int scale;
private String text;
// getters, setters, default constructor
}
在我的控制器中,我提供了一个新的 sn-p 进行编辑,当用户单击“保存”时,将其存储到我的数据库中,然后返回另一个。目前sn-p的句子列表为空:
@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());
}
在我的 sn-p.jsp 中,我想显示带有比例的 sn-p 句子,并在保存时将带有句子和比例的 sn-p 传递给控制器进行存储:
<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>
我想我必须找到使用路径属性的正确方法,但我想不通。
【问题讨论】:
标签: java spring jsp spring-mvc jstl