当前位置: 首页 > 知识库问答 >
问题:

Neo4j插入的Thymeleaf集合绑定

燕英逸
2023-03-14

我一直在开发一个带有Thymeleaf UI的Spring MVC应用程序。对于Neo4j处理实体,我使用包含属于类POST的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;

}

不要误会我,我知道我想使用Set将Neoimage存储在Neopost类中。问题不在于持久性或任何东西,而是我想从thymeleaf表单传递输入结果。

 <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>

然后,当我访问post请求时,问题按预期填写在模型中,但neoimageset不包含两个输入字段中的两个字符串。我听说,数据绑定在某种程度上是不可能的一套与百里香。如果您需要进一步说明,我完全理解,谢谢您的帮助。

共有1个答案

姜明贤
2023-03-14

我已使用以下包装器方法解决了此问题:

/**
 * 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,而不是实际的集合。

 类似资料:
  • 我的问题是关于thymeleaf数据对象绑定。表单标记中有th:object=“${newPost}”。Thymeleaf看到它,当我提交我的表单时,它将每个,绑定到这个对象的字段。 但是如果我从type='text'更改为type='anything else'>thymeleaf不会映射任何内容。我如何使thymeleaf绑定其他标签,如等到我的对象?

  • 我试图将视图与结合使用。我有一个实体,拥有一组电子邮件,我想在Thymeleaf模板中访问这些电子邮件: 在用户中。斯卡拉 在一些模板中。html 问题是Spring表达式求值器(Thymeleaf将表达式求值任务委托给它)只理解Java集合类型,因此试图获取Scala列表的第一个元素()/>)失败,出现异常。 我试图深入SpEL源代码,找到一个可以添加Java到Scala转换的地方。似乎如果我可

  • 这是我的控制器: 我的观点是: 和VO类:

  • 我的批处理插入器有奇怪的问题。批处理插入器工作良好,但当我从该位置启动服务器时,CYPHER无法筛选属性。 Query返回所有节点。但当我尝试基于任何属性筛选它时,它不会返回任何行。查询不返回任何内容。 如果我运行run SET命令来更新属性,那么我就能够很好地筛选它。看起来像是索引问题,但无法准确计算。

  • 问题内容: 为什么某些集合数据结构不能保持插入顺序?与保持插入顺序相比,有什么特别的事情?如果不维持订单,我们会有所收获吗? 问题答案: 性能。如果您想要原始的插入顺序,则可以使用LinkedXXX类,它们在插入顺序中维护着一个附加的链表。大多数时候,您都不在乎,因此您使用HashXXX,或者想要自然订单,因此您使用TreeXXX。在这两种情况下,您为什么都要支付链接列表的额外费用?

  • 我正在实验/学习Spring数据neo4j。我有一个非常简单的应用程序,可以存储来自推特的推文。请参阅下面的片段。 问题是,存储哈希标签的最佳方式是什么,这样我就可以快速获取它们所属的推文?我能想到的是要么在Set上使用@索引,要么实际上创建一个单独的标签NodeEntity,并在它和推文之间建立关系。我找不到在NodeEntity中索引集合的任何留档,所以我不确定是否在set对象上创建了索引,或