Please tell me how to properly bind the HashSet collection.
There is an App class that contains Set, which is subsequently created as new HashSet (). In the controller, there is a description of the GET method, as a parameter of the model, I transfer to the newly created instance of App, with an already initialized HashSet, which has 1 element
@RequestMapping(value = "/admin/appNew", method = RequestMethod.GET)
public ModelAndView appNew(HttpServletRequest request) throws IOException {
ModelAndView modelAndView = new ModelAndView();
App app = CreateNewApp();
modelAndView.addObject("app", app);
modelAndView.setViewName("admin/appEdit");
return modelAndView;
}
private App CreateNewApp() throws IOException {
Version version = new Version();
version.setComment("AppVersion");
version.setNumber(0);
version.setLicense_value(0);
App app = new App();
app.setName("Новое приложение");
app.setAutor("Автор");
app.setVersions(new HashSet<Version>());
app.getVersions().add(version);
return app;
}
on the side of the view, I do an iteration on HashSet
<form th:object="${app}" th:action="@{appNew}" method="POST" class="form-signin">
<input type="text" th:field="*{name}" th:placeholder="Name" class="form-control" /> <br/>
<input type="text" th:field="*{autor}" th:placeholder="Autor" class="form-control" /> <br/>
<input type="number" th:field="*{active}" th:placeholder="Active" class="form-control" /> <br/>
<div th:object="${app}" class="card mb-4 box-shadow">
<div th:each="ver, step :${app.versions}">
<div th:object="${ver}">
<input th:field="*{comment}" th:value="${ver.getComment()}" class="card-text"></input>
</div>
</div>
</div>
<button class="btn btn-lg btn-primary btn-block" name="Submit" value="create" type="Submit" th:text="Create"></button>
</form>
Collection values are correctly filled in th: value and displayed, but when you try to bind each through th: field for a subsequent POST request, an error occurs
Caused by: java.lang.IllegalStateException: Neither BindingResult nor plain target object for bean name 'ver' available as request attribute
if you remove th: field = "* {comment}", then a list of the entire HashSet will be displayed, but the data will not go to the controller processing the POST request since there will be no binding
Aucun commentaire:
Enregistrer un commentaire