2

Подскажите пожалуйста, как корректно привязать коллекцию HashSet

Имеется класс App, который содержит Set, которая в последующем создаётся как new HashSet(). В контроллере имеется описание GET метода, в качестве параметра модели я передаю во View вновь созданный экземпляр App, с уже проинициализированным HashSet , который имеет 1 элемент

@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;
}

на стороне View , я делаю итерацию по 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>

Значения коллекции корректно заполняются в th:value и отображаются, но при попытке привязки каждого через th:field для последующего POST запроса возникает ошибка

Caused by: java.lang.IllegalStateException: Neither BindingResult nor plain target object for bean name 'ver' available as request attribute

если убрать th:field="*{comment}", то будет выведен список всего HashSet , но данные не уйдут в контроллер обрабатывающий POST запрос т.к. будет отсутствовать привязка

1
  • Когда возникает такое исключение?
    – Roman C
    8 окт 2018 в 7:14

0

Ваш ответ

By clicking “Отправить ответ”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Посмотрите другие вопросы с метками или задайте свой вопрос.