从控制器我发送像这样的BinderPlaceOrder对象
model.addAttribute("addNewBinderPlaceOrder", new BinderPlaceOrder());
我的Thymeleaf页面是,
<form class="addBinderPlaceOrderForm" role="form" action="#" th:action="@{/binderPlaceOrder/new-binderPlaceOrder}" th:object="${addNewBinderPlaceOrder}" method="post">
<div class="form-group">
<label>Book</label>
<select class="form-control" th:field="*{binderOrderItemDetails.book}">
<option th:if="${book} == null" value=" " >Select Book</option>
<option th:each="book : ${allBook}"
th:value="${book.id}"
th:text="${book.name}">
</option>
</select>
</div>
<form>
&#13;
这是控制器,
@Controller
@RequestMapping("/binderPlaceOrder")
public class BinderPlaceOrderController{
@Autowired
BinderPlaceOrderService binderPlaceOrderService;
@Autowired
BookService bookService;
@RequestMapping(value="/new-binderPlaceOrder", method = RequestMethod.GET)
public String newBinderPlaceOrder(Model model){
model.addAttribute("addNewBinderPlaceOrder", new BinderPlaceOrder());
model.addAttribute("allBook", bookService.getAllBooks();
model.addAttribute("addNewBinderOrderItemDetails", new BinderOrderItemDetails());
return "user/binderPlaceOrder/new";
}
}
在运行期间,我收到以下错误
org.springframework.beans.NotReadablePropertyException:无效的属性&#39; binderOrderItemDetails.book&#39; bean类[PublisherInventory.model.user.BinderPlaceOrder]:Bean属性&#39; binderOrderItemDetails.book&#39;不可读或getter方法无效:getter的返回类型是否与setter的参数类型匹配?
以下是课程。
请注意,每个班级都有自己的Id属性以及其他属性。为了便于阅读,省略了这些内容。
这是BinderPlaceOrder Class,
@Entity
public class BinderPlaceOrder implements Comparator<BinderPlaceOrder> {
@OneToMany(mappedBy = "binderPlaceOrder", fetch = FetchType.LAZY, cascade = CascadeType.ALL)
private List<BinderOrderItemDetails> binderOrderItemDetails;
public List<BinderOrderItemDetails> getBinderOrderItemDetails() {
return binderOrderItemDetails;
}
public void setBinderOrderItemDetails(List<BinderOrderItemDetails> binderOrderItemDetails) {
this.binderOrderItemDetails = binderOrderItemDetails;
}
}
这是BinderOrderItemDetails类
@Entity
@Indexed
public class BinderOrderItemDetails {
@OneToOne(fetch = FetchType.EAGER,cascade = CascadeType.ALL)
@JoinColumn(name="bookId")
private Book book;
@ManyToOne(fetch = FetchType.EAGER, cascade = CascadeType.ALL)
@JoinColumn(name="binderPlaceOrderId")
private BinderPlaceOrder binderPlaceOrder;
public Book getBook() { return book; }
public void setBook(Book book) { this.book = book; }
public BinderPlaceOrder getBinderPlaceOrder() { return binderPlaceOrder; }
public void setBinderPlaceOrder(BinderPlaceOrder binderPlaceOrder) {
this.binderPlaceOrder = binderPlaceOrder; }
}
这是Book Class
@Entity
@Indexed
public class Book implements Comparator<Book> {
@Column(name = "name")
@NotNull
@NotBlank
private String name;
public String getName() { return name; }
public void setName(String name) { this.name = name;}
}
你能否告诉我我做错了什么或如何解决?
提前致谢。
答案 0 :(得分:1)
正如@JBNizet指出的那样,你的 BinderPlaceOrder 已经
public List<BinderOrderItemDetails> getBinderOrderItemDetails() {
return binderOrderItemDetails;
}
这是一个列表,因此
th:field="*{binderOrderItemDetails.book}"
不正确,
但是你想插入第一个元素
th:field="${binderOrderItemDetails[0].book}"