我知道已经有人提出了有关此事的问题,但我找到的答案都没有解决我的问题。
我的数据库上有多对多关系。我正在使用 JPA 和 Hibernate 来创建和更改我的表。这是我的模型类:
Book.java
@Entity
@Table(name="tb_books")
public class Book implements Serializable {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id", updatable = false, nullable = false, insertable = false)
private Integer id;
@Column(name = "title", nullable = false, length = 255)
private String title;
@Column(name = "author", nullable = false, length = 255)
private String author;
@ManyToMany(cascade = {CascadeType.PERSIST, CascadeType.MERGE}, fetch = FetchType.EAGER)
@JoinTable(name = "book_tag",
joinColumns = { @JoinColumn(name = "fk_book") },
inverseJoinColumns = { @JoinColumn(name = "fk_tag") })
private List<Tag> tags;
//getters, setters, equals and hash methods...
}
标签.java
@Entity
@Table(name="tb_tags")
public class Tag implements Serializable {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id", updatable = false, nullable = false)
private Integer id;
@Column(name = "description", nullable = false, length = 255)
private String description;
//getters, setters, equals and hash methods...
}
我正在尝试使用 Spring MVC 使用来自 JSP 的数据在我的 book_tag 表上进行插入和更新。这是我的 Controller 和插入页面:
BookController.java
@Controller
@RequestMapping(value = "/book")
public class BookController {
@Autowired
private BookService bookService;
/*
Controller method that gets us to the adding book page.
*/
@RequestMapping(value = "/add", method = RequestMethod.GET)
public ModelAndView addBookPage() {
List<Tag> tags = tagService.getTags(); //all tags from the database
ModelAndView modelAndView = new ModelAndView("form-book");
modelAndView.addObject("book", new Book());
modelAndView.addObject("tags", tags);
return modelAndView;
}
@RequestMapping(value = "/add", method = RequestMethod.POST)
public ModelAndView addBookProcess(@ModelAttribute Book book) {
bookService.addBook(book);
return new ModelAndView("redirect:/book/add");
}
//...
}
form-book.jsp
<form:form method="POST" modelAttribute="book" action="${pageContext.request.contextPath}/book/add.html">
<table>
<tbody>
<tr>
<td>Title:</td>
<td><form:input path="title" autocomplete="off" /></td>
</tr>
<tr>
<td>Author:</td>
<td><form:input path="author" autocomplete="off" /></td>
</tr>
<tr>
<td colspan="2">
<form:checkboxes path="tags"
items="${tags}"
itemLabel="description"
itemValue="id"/>
</td>
</tr>
<tr>
<td><input type="submit" value="Add" /></td>
<td><input type="button" onclick="location.href = '${pageContext.request.contextPath}/index'" value="Cancel"/></td>
</tr>
</tbody>
</table>
</form:form>
一切工作正常,除了当我尝试更新或插入选中一个或多个复选框的书籍时。当我尝试这样做时,我只是得到一个“错误请求”页面,我想是因为我在数据绑定(bind)方面做了一些错误。
这是我应该处理这种情况的方式吗?有没有更好的办法?我做错了什么?
最佳答案
显然,来自复选框的列表实际上是一个字符串数组,它无法按照模型类的定义转换为标签列表。
我通过创建自定义 PropertyEditor 解决了我的问题,它允许我将复选框与标签列表绑定(bind)。
public class TagPropertyEditor extends PropertyEditorSupport {
private TagService tagService;
public TagPropertyEditor(TagService tagService){
this.tagService = tagService;
}
@Override
public String getAsText() {
return ((Tag) getValue()).getId().toString();
}
@Override
public void setAsText(String incomingId) throws IllegalArgumentException {
Tag tag = tagService.getTag(Integer.valueOf(incomingId));
setValue(tag);
}
}
然后在我的 BookController 上注册:
//...
@InitBinder
public void initBinder(WebDataBinder binder){
binder.registerCustomEditor(Tag.class, new TagPropertyEditor(tagService));
}
这是我的 JSP 上的 checkboxes 标记:
<form:checkboxes path="tags"
items="${tags}"
itemLabel="description"
itemValue="id"/>
关于java - 使用列表和复选框的 Spring MVC 数据绑定(bind),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46123767/