Переплет из тимелина - PullRequest
0 голосов
/ 19 мая 2018

Я работал над приложением Spring MVC с интерфейсом Thymeleaf.Для обработки объектов Neo4j я использую Set, содержащий NeoImages, принадлежащий классу сообщений:

@Data
@NodeEntity
@Builder
@AllArgsConstructor
@NoArgsConstructor
public class NeoPost {

    @Id
    @GeneratedValue
    Long postId;

    @NotNull
    @NotBlank
    @Size(min = 1, max = 600)
    String question;

    /**
     * Images that are involved in that post
     */
    @NotEmpty
    @Size(min = 2)
    @Relationship(type = "STARES", direction = Relationship.OUTGOING)
    Set<Neoimage> neoimageSet = new HashSet<>();

    /**
     * User that made this post
     */
    @Relationship(type = "OWNS", direction = Relationship.INCOMING)
    NeoUser user;


    /**
     * Users that somehow in a way possible described in Userinteractiontype enum
     * with this current post.
     */
    @Relationship(type = "INTERACTED_WITH", direction = Relationship.INCOMING)
    Set<NeoInteraction> incomingInteractions = new HashSet<>();


}

Здесь класс типа NeoImage:

@Data
@Builder
@AllArgsConstructor
@NoArgsConstructor
@NodeEntity
public class Neoimage {

    @Id
    @GeneratedValue
    Long imageId;

    //For testing purposes use this type of information
    @ImageUrlValidator
    String imageFull;

}

Не поймите меня неправильно, язнаю, что я хочу использовать набор для хранения Neoimages в классе Neopost.Проблема не в постоянстве или чем-то еще, но я хочу доставить входные результаты из формы тимьяна.

 <form autocomplete="off" action="#" th:action="@{/postQuestion}"
                          th:object="${neoPost}" method="post" role="form">

                        <div class="form-group">
                            <div class="col-sm-12">

                                <label th:if="${#fields.hasErrors('question')}" th:errors="*{question}"
                                       class="validation-message"></label>

                                <input type="text" th:field="*{question}" placeholder="Question"
                                       class="form-control" />
                            </div>
                        </div>

                        <div class="form-group">
                            <div class="col-sm-12">
                                <input type="text" th:field="*{neoimageSet[0].imageFull}"
                                       placeholder="Image 1" class="form-control" /> <label
                                    th:if="${#fields.hasErrors('neoimageSet[0].imageFull')}" th:errors="*{neoimageSet[0].imageFull}"
                                    class="validation-message"></label>
                            </div>
                        </div>

                        <div class="form-group">
                            <div class="col-sm-12">
                                <input type="text" th:field="*{neoimageSet[1].imageFull}"
                                       placeholder="Image 1" class="form-control" /> <label
                                    th:if="${#fields.hasErrors('neoimageSet[1].imageFull')}" th:errors="*{neoimageSet[1].imageFull}"
                                    class="validation-message"></label>
                            </div>
                        </div>

                        <div class="form-group">
                            <div class="col-sm-12">
                                <button type="submit" class="btn btn-outline-secondary my-2 my-lg-0 loginButton">Upload Post</button>
                            </div>
                        </div>

                        <span th:utext="${successMessage}"></span>


                    </form>

Когда я затем получаю доступ к почтовому запросу, вопрос заполняется в модели, как и ожидалось, ноneoimageset не содержит две строки из двух полей ввода.Я слышал, что привязка данных как-то невозможна для набора с тимелистом.Я могу полностью понять, если вам нужны какие-либо дальнейшие уточнения, спасибо за помощь.

1 Ответ

0 голосов
/ 21 мая 2018

Я решил эту проблему, используя метод-обертку в:

/**
 * Wrapper method since set elements cannot be accessed by index which is needed in thymeleaf
 */
@Transient
AutoPopulatingList<Neoimage> neoImageWrapperList = new AutoPopulatingList(Neoimage.class);

public AutoPopulatingList<Neoimage> getNeoImageWrapperList() {
    //Copy content to neoimageset
    this.neoImageWrapperList.forEach(neoimage -> neoimageSet.add(neoimage));
    return this.neoImageWrapperList;
}

Теперь я просто вызываю neoImageWrapperList вместо фактического набора.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...