【发布时间】:2015-04-01 15:27:28
【问题描述】:
我正在尝试仅检索具有特定标签的条目(帖子)。一个条目可以有多个标签,所以我使用对象列表。但是我不知道如何在 Controller 类中构造一个正确的命令,实际上我怕我完全迷失了这里。
条目实体如下所示:
@Entity
public class BlogEntry {
@Id
@GeneratedValue
private Integer id;
private String title;
@Column(name = "published_date")
private Date publishedDate;
@ManyToMany
@JoinTable
private List<TagBlog> blogTags; /* Multiple tags to one entry */
还有我的标签实体:
@Entity
public class TagBlog {
@Id
@GeneratedValue
private Integer id;
private String tag;
@ManyToMany(mappedBy="blogTags")
private List<BlogEntry> entries;
在我的 EntryService 类中,我想执行这种排序“findByTagBlogIn”,我希望它会返回具有特定标签的帖子列表。
public List<BlogEntry> findAllByTags(List<TagBlog> tag){
List<BlogEntry> blogEntry = entryRepository.findByTagBlogIn(tag);
return blogEntry;
}
但我不知道如何在 Controller 类中引用它。如何仅检索具有特定标签的条目?像这样的东西?但是如何将标签列表作为参数传递,也许应该是字符串?
@RequestMapping(value="/welcome")
public String retrieveTaggedEntry(Model model, ?List of tags?){
model.addAttribute("entriesWithTag", entryService.findAllByTags(TagBlog tag));
return "redirect:/welcome.html";
}
在welcome.jsp 文件中,我想遍历已分配给特定条目(帖子)的整个标签列表,如下例所示(箭头之间的“--->
<c:forEach items="${entries}" var="entry"> <!--"entries" refers to List of BlogEntry-->
<table>
<tr>
<td>Entry No. ${entry.id }</td>
<td>${entry.title}</td>
<td>
Tags: ---> #${entry.? blogTag.tag ?}, <---
</td>
<td>Published: ${entry.publishedDate}</td>
<td>
<spring:url value="/delete_entry/${entry.id}.html" var="url" />
<a href="${url}">Delete</a>
</td>
</tr>
</table>
</c:forEach>
通过稍后解决,我想通过拥有特定标签的条目执行排序(通过映射 spring:url value="/tag/${some_tag_as_a_String}.html")。
也许有一种更简单的方法可以只返回带有特定标签的帖子?但我想用我在这里得到的东西对我来说会更容易。无论如何,我们感谢您提供的任何解决方案。
如果需要,我愿意添加任何其他信息。
【问题讨论】:
标签: java spring jsp spring-data spring-data-jpa