我一直在使用Thymeleaf UI开发Spring MVC应用程序。对于Neo4j处理实体,我使用包含属于类帖子的NeoImages的Set:
@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来将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>
当我然后访问post请求时,问题按预期填充在模型中,但neoimageset不保存两个输入字段中的两个字符串。我听说数据绑定在某种程度上不可能与百里香的集合。如果您需要进一步详细说明,我完全可以理解,谢谢您的帮助。
答案 0 :(得分:0)
我使用了一个包装器方法解决了这个问题:
/**
* 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而不是实际的设置。