我一直在努力解决如何在我使用 Spring 3 和 Hibernate 4 构建的 Web 应用程序中实现创建多对多关系的表单。我正在尝试构建一个带有标记系统的简单博客工具。我创建了一个与模型BlogPost
具有多对多关系的模型Tags
。当我创建一个新BlogPost
对象时,标签的 Web 表单输入是单行文本输入。我希望能够用空格分割这个文本字符串并用它来创建Tag
对象。或者,在编辑现有的 时BlogPost
,我希望能够获取与关联Set
的对象并将其转换为Tag
BlogPost
String
用作输入元素的值。Tag
我的问题是使用我的表单 在文本输入和引用的对象集之间进行转换。
绑定/获取/更新与 Web 表单的多对多关系的最佳实践是什么?有没有一种我不知道的简单方法可以做到这一点?
更新
我决定,如下面的答案所建议的那样,手动处理String
表单中的标记值与Set<Tag>
对象模型所需的对象之间的对象转换。这是最终的工作代码:
编辑BlogPost.jsp
...
<div class="form-group">
<label class="control-label col-lg-2" for="tagInput">Tags</label>
<div class="col-lg-7">
<input id="tagInput" name="tagString" type="text" class="form-control" maxlength="100" value="${tagString}" />
</div>
<form:errors path="tags" cssClass="help-inline spring-form-error" element="span" />
</div>
....
博客控制器.java
@Controller
@SessionAttributes("blogPost")
public class BlogController {
@Autowired
private BlogService blogService;
@Autowired
private TagService tagService;
@ModelAttribute("blogPost")
public BlogPost getBlogPost(){
return new BlogPost();
}
//List Blog Posts
@RequestMapping(value="/admin/blog", method=RequestMethod.GET)
public String blogAdmin(ModelMap map, SessionStatus status){
status.setComplete();
List<BlogPost> postList = blogService.getAllBlogPosts();
map.addAttribute("postList", postList);
return "admin/blogPostList";
}
//Add new blog post
@RequestMapping(value="/admin/blog/new", method=RequestMethod.GET)
public String newPost(ModelMap map){
BlogPost blogPost = new BlogPost();
map.addAttribute("blogPost", blogPost);
return "admin/editBlogPost";
}
//Save new post
@RequestMapping(value="/admin/blog/new", method=RequestMethod.POST)
public String addPost(@Valid @ModelAttribute BlogPost blogPost,
BindingResult result,
@RequestParam("tagString") String tagString,
Model model,
SessionStatus status)
{
if (result.hasErrors()){
return "admin/editBlogPost";
}
else {
Set<Tag> tagSet = new HashSet();
for (String tag: tagString.split(" ")){
if (tag.equals("") || tag == null){
//pass
}
else {
//Check to see if the tag exists
Tag tagObj = tagService.getTagByName(tag);
//If not, add it
if (tagObj == null){
tagObj = new Tag();
tagObj.setTagName(tag);
tagService.saveTag(tagObj);
}
tagSet.add(tagObj);
}
}
blogPost.setPostDate(Calendar.getInstance());
blogPost.setTags(tagSet);
blogService.saveBlogPost(blogPost);
status.setComplete();
return "redirect:/admin/blog";
}
}
//Edit existing blog post
@Transactional
@RequestMapping(value="/admin/blog/{id}", method=RequestMethod.GET)
public String editPost(ModelMap map, @PathVariable("id") Integer postId){
BlogPost blogPost = blogService.getBlogPostById(postId);
map.addAttribute("blogPost", blogPost);
Hibernate.initialize(blogPost.getTags());
Set<Tag> tags = blogPost.getTags();
String tagString = "";
for (Tag tag: tags){
tagString = tagString + " " + tag.getTagName();
}
tagString = tagString.trim();
map.addAttribute("tagString", tagString);
return "admin/editBlogPost";
}
//Update post
@RequestMapping(value="/admin/blog/{id}", method=RequestMethod.POST)
public String savePostChanges(@Valid @ModelAttribute BlogPost blogPost, BindingResult result, @RequestParam("tagString") String tagString, Model model, SessionStatus status){
if (result.hasErrors()){
return "admin/editBlogPost";
}
else {
Set<Tag> tagSet = new HashSet();
for (String tag: tagString.split(" ")){
if (tag.equals("") || tag == null){
//pass
}
else {
//Check to see if the tag exists
Tag tagObj = tagService.getTagByName(tag);
//If not, add it
if (tagObj == null){
tagObj = new Tag();
tagObj.setTagName(tag);
tagService.saveTag(tagObj);
}
tagSet.add(tagObj);
}
}
blogPost.setTags(tagSet);
blogPost.setPostDate(Calendar.getInstance());
blogService.updateBlogPost(blogPost);
status.setComplete();
return "redirect:/admin/blog";
}
}
//Delete blog post
@RequestMapping(value="/admin/delete/blog/{id}", method=RequestMethod.POST)
public @ResponseBody String deleteBlogPost(@PathVariable("id") Integer id, SessionStatus status){
blogService.deleteBlogPost(id);
status.setComplete();
return "The item was deleted succesfully";
}
@RequestMapping(value="/admin/blog/cancel", method=RequestMethod.GET)
public String cancelBlogEdit(SessionStatus status){
status.setComplete();
return "redirect:/admin/blog";
}
}
BlogPost.java
@Entity
@Table(name="BLOG_POST")
public class BlogPost implements Serializable {
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
@Column(name="POST_ID")
private Integer postId;
@NotNull
@NotEmpty
@Size(min=1, max=200)
@Column(name="TITLE")
private String title;
...
@ManyToMany(fetch = FetchType.LAZY, cascade = {CascadeType.ALL})
@JoinTable(name="BLOG_POST_TAGS",
joinColumns={@JoinColumn(name="POST_ID")},
inverseJoinColumns={@JoinColumn(name="TAG_ID")})
private Set<Tag> tags = new HashSet<Tag>();
...
public Set<Tag> getTags() {
return tags;
}
public void setTags(Set<Tag> tags) {
this.tags = tags;
}
}
标签.java
@Entity
@Table(name="TAG")
public class Tag implements Serializable {
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
@Column(name="TAG_ID")
private Integer tagId;
@NotNull
@NotEmpty
@Size(min=1, max=20)
@Column(name="TAG_NAME")
private String tagName;
@ManyToMany(fetch = FetchType.LAZY, mappedBy="tags")
private Set<BlogPost> blogPosts = new HashSet<BlogPost>();
public Integer getTagId() {
return tagId;
}
public void setTagId(Integer tagId) {
this.tagId = tagId;
}
public String getTagName() {
return tagName;
}
public void setTagName(String tag) {
this.tagName = tag;
}
public Set<BlogPost> getBlogPosts() {
return blogPosts;
}
public void setBlogPosts(Set<BlogPost> blogPosts) {
this.blogPosts = blogPosts;
}
}