初学春天的家伙在这里。我需要将数据从spring jsp表单传递给controller。问题是我的对象属性之一是Set,我必须将数组数据传递给控制器。我是用jQuery做的。
jsp'的片段代码形式:
<form:form action="savePost" method="post" commandName="blogpost">
<tr><td>Title: </td> <td><form:input path="title"/></td></tr>
<tr><td>Content: </td> <td><form:input path="content"/></td></tr>
<tr><td>Author: </td> <td><form:input path="author"/></td></tr>
<tr><td>Tags: </td> <td><form:input path="tags" id="tags"/></td></tr>
<tr><td><input type="submit" value="Blog post save"></td></tr>
</form:form>
和我的bean的代码:
@Entity
@Table(name="posts")
public class BlogPost {
@Id
@Column(name = "id")
@GeneratedValue(strategy = GenerationType.AUTO)
private long id;
@Column(name="post_title")
private String title;
@Column(name="post_content")
private String content;
@Column(name="post_author", nullable = false)
private String author;
@OneToMany(mappedBy = "blogPost")
private List<Comment> comments;
@ManyToMany
@JoinTable(name="BLOGPOST_TAG",
joinColumns = {@JoinColumn(name = "POST_ID", referencedColumnName = "id")},
inverseJoinColumns = {@JoinColumn(name= "TAG_ID", referencedColumnName = "id")})
private Set<Tag> tags;
public BlogPost(){}
public BlogPost(String title, String content, String author) {
this.title = title;
this.content = content;
this.author = author;
}
public BlogPost(String title, String content, String author, Set<Tag> tags) {
this.title = title;
this.content = content;
this.author = author;
this.tags = tags;
}
public BlogPost(long id, String title, String content, String author, Set<Tag> tags) {
this.id = id;
this.title = title;
this.content = content;
this.author = author;
this.tags = tags;
}
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
public String getAuthor() {
return author;
}
public void setAuthor(String author) {
this.author = author;
}
public List<Comment> getComments() {
return comments;
}
public void setComments(List<Comment> comments) {
this.comments = comments;
}
public String toString(){
return "ID: "+getId()+", title: "+getTitle()+", content: "+getContent()+", author: "+getAuthor()+"\n";
}
public Set<Tag> getTags() {
return tags;
}
public void setTags(Set<Tag> tags) {
this.tags = tags;
}
}
第二个:
@Entity
@Table(name="tags")
public class Tag {
@Id
@Column(name = "id")
private long id;
@Column(name="tag_content")
private String content;
@ManyToMany(mappedBy = "tags")
private List<BlogPost> blogposts;
public Tag(){}
public Tag(String content){
this.content = content;
}
public Tag(long id, String content) {this.id = id; this.content = content; }
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
public List<BlogPost> getBlogposts() {
return blogposts;
}
public void setBlogposts(List<BlogPost> blogposts) {
this.blogposts = blogposts;
}
}
修改 控制器的代码:
@PostMapping(value = {"savePost","/updatePost"})
public String createOrUpdatePostPOST (@Valid @ModelAttribute("blogpost") BlogPost blogpost,
BindingResult bindingResult, HttpServletRequest request, Model model,
@RequestParam(value="tagArray[]") List<String> myArray){
//Loop to show data from jsp
for (String tag : myArray){
System.out.println(tag);
}
String mappingValue = request.getServletPath();
blogPostValidator.validate(blogpost, bindingResult);
List<BlogPost> listOfBlogPostsToGetLastindex = repo.findFirstByOrderByIdDesc();
long newCommentId = listOfBlogPostsToGetLastindex.get(0).getId()+1;
if (bindingResult.hasErrors()) {
if (mappingValue.contains("/savePost")){
model.addAttribute("status", 0);
return EDIT_PAGE_VIEW;
}
else {
model.addAttribute("status", 2);
return EDIT_PAGE_VIEW;
}
}
else{
blogpost.setId(newCommentId);
repo.save(utilityService.returnCapitalizedPostObject(blogpost));
return "redirect:/create-or-update-success";
}
}
现在的问题是虽然我可以使用System.out
查看传递的数据,但我收到错误Required List parameter 'tagArray[]' is not present
。知道我做错了什么吗?