My Controller:
@Controller
public class MainController {
@GetMapping("/")
public String mainPage() {
return "main";
}
@RequestMapping(value = "/", params = {"addRow"})
public String addRow(ModelForCheck modelForCheck, BindingResult bindingResult) {
modelForCheck.getVariables().add(new Variable());
return "main";
}
}
My model class that I used to test my code:
public class ModelForCheck {
private List<Variable> variables = new ArrayList<>();
public List<Variable> getVariables() {
return variables;
}
public void setVariables(List<Variable> variables) {
this.variables = variables;
}
}
My model class:
public class Variable {
private String name;
private String type;
public Variable() {
}
public Variable(String name, String type) {
this.name = name;
this.type = type;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
}
My html page (main.html):
<!DOCTYPE html>
<html lang="en" xmlns="http://www.w3.org/1999/xhtml" xmlns:th="https://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>Hello!</title>
</head>
<body>
<form th:action="@{/}" method="post">
<h3>Variables:</h3>
<button type="submit" name="addRow">Add Row</button>
<table>
<tr th:each="variable, variableStat : *{variables}">
<td><input type="text" th:field="*{variables[__${variableStat.index}__].name}" placeholder="name"></td>
<td><input type="text" th:field="*{variables[__${variableStat.index}__].type}" placeholder="type..."></td>
</tr>
</table>
<br>
<input type="submit" value="Generate!">
</form>
</body>
</html>
I click on the "Add Row" button, but for some reason there is no reaction at all from the application. There are no error messages in the logs, nor any other messages. I know for sure that the "addRow" method is launched, since I checked it in the debug
I think It is doesn't work because you don't add modelForCheck
to Thymeleaf model in addRow
method in your controller.