Ich folge angegebenen Link einreichen Tabellendaten vorzulegen in der Datenbank gespeichert werdenWie Tabellendaten im Frühjahr Thymeleaf
http://viralpatel.net/blogs/spring-mvc-multi-row-submit-java-list/
Aber der Unterschied zwischen bestimmtem Link und meiner Implementierung ist, dass das Front-End Link verwendet JSTL (JSP), während ich Thymeleaf bin mit (HTML)
Im Folgenden sind die Dateien
HTML-Formular verwendet werden:
<form method="POST" th:action="@{/updateAllRules}" th:field="${ruleForm}">
<table>
<thead>
<tr>
<th>S No</th>
<th>Title</th>
<th>Body</th>
</tr>
</thead>
<tbody>
<tr th:each="ruleModel,iteration : ${allRules}">
<td th:text="${ruleModel.id}"></td>
<td><input type="text" th:name="${'rule'+iteration.index+'.title'}" th:value="${ruleModel.title}"></td>
<td><input type="text" th:name="${'rule'+iteration.index+'.body'}" th:value="${ruleModel.body}"></td>
</tr>
</tbody>
</table>
<br>
<input type="submit" value="Update All">
</form>
Model-Klasse:
public class Rule {
private Integer id;
private Date timestamp;
private String title;
private String body;
// constructor and Getter/Setters
}
Formularklasse:
public class RuleForm {
private List<Rule> rule;
public List<Rule> getRule() {
return rule;
}
public void setRule(List<Rule> rule) {
this.rule = rule;
}
}
Controller-Methode:
@RequestMapping(value = "/updateAllRules", method = RequestMethod.POST)
public String updateAllRules(@ModelAttribute("ruleForm") RuleForm ruleForm) throws IOException
{
System.out.println(ruleForm); // this prints [email protected]
System.out.println(ruleForm.getRule()); //this prints null
return "redirect:/admin";
}
Bitte lassen Sie mich wissen, was ich bin fehlt.
UPDATE 1:
Änderungen vorgenommen, wie vorgeschlagen. Mein neues HTML-Formular lautet wie folgt:
<form method="POST" th:action="@{/updateAllRules}" th:object="${ruleForm}">
<table>
<thead>
<tr>
<th>S No</th>
<th>Title</th>
<th>Body</th>
</tr>
</thead>
<tbody>
<tr th:each="rule,iteration : ${ruleForm}">
<td th:field="*{rule[__${iteration.index}__].id}"></td>
<td><input type="text" th:field="*{rule[__${iteration.index}__].title}"></td>
<td><input type="text" th:field="*{rule[__${iteration.index}__].body}"></td>
</tr>
</tbody>
</table>
<br>
<input type="submit" value="Update All">
</form>
Beim Ausführen dieser Änderungen wird die folgende Ausnahme empfangen, wenn die Seite geladen wird.
org.springframework.expression.spsel.SpelEvaluationException: EL1008E: Eigenschaft oder Feld 'rule' kann nicht auf Objekt vom Typ 'java.util.ArrayList' gefunden werden - vielleicht nicht öffentlich?
Bitte beachten Sie, dass ich Original-Liste in Modell Attribut "RuleForm" beim Laden der Seite senden. Sobald die Seite die Daten lädt und der Benutzer Änderungen vornimmt, möchte ich die Tabelle vollständig zurück an den Controller senden.
Got meine Antwort, müssen Daten über Wrapper wie in gegebenen Link senden https://StackOverflow.com/Questions/36500731/How-to-bind-an-Object-list-with-Thymeleaf –