![](/img/trans.png)
[英]Spring MVC - Model object not returning to view from controller- JSP
[英]How to map a complex structure in jsp view to a model object in spring MVC
我第一次使用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
}
在我的控制器中,我提供了一個新的代碼片段進行編輯,當用戶點擊“保存”將其存儲到我的數據庫中,然后返回另一個。 目前,片段的句子列表為空:
@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());
}
在我的snippet.jsp中,我想用它們的比例顯示片段句子,並在保存時,將帶有句子和縮放的片段傳遞給控制器進行存儲:
<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>
我想我必須找到使用path屬性的正確方法,但我無法弄明白。
JSTL c:forEach
標記提供屬性varStatus
,它將循環狀態公開給指定的變量。 引用varStatus
的index
以獲取當前循環的索引,並使用該索引指定要綁定或顯示的集合項的索引。
<c:forEach var="sentence" items="${snippet.sentences}" varStatus="i">
<tr>
<td>${sentence.id}</td>
<td>${sentence.text}</td>
<td>
<form:input type="range"
name="snippet.sentences[${i.index}].scale"
path="sentences[${i.index}].scale"
min="0" max="5"
/></td>
</tr>
</c:forEach>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.