Java 从thymeleaf对象获取值
TL:DR 与上面的问题非常相似,但我的值“thepublisher”将所选值传递为空,并且我已经完成了建议的解决方案。 我有一个图书表单(添加)页面,如下所示:Java 从thymeleaf对象获取值,java,spring,spring-boot,spring-mvc,thymeleaf,Java,Spring,Spring Boot,Spring Mvc,Thymeleaf,TL:DR 与上面的问题非常相似,但我的值“thepublisher”将所选值传递为空,并且我已经完成了建议的解决方案。 我有一个图书表单(添加)页面,如下所示: <form th:action="@{/book/save}" th:object="${book}" method="POST"> <!-- Add hidden form field to handle u
<form th:action="@{/book/save}"
th:object="${book}" method="POST">
<!-- Add hidden form field to handle update -->
<input type="hidden" th:field="*{id}" />
<input type="text" th:field="*{bookName}"
class="form-control mb-4 col-4" placeholder="First name">
<input type="text" th:field="*{bookDescription}"
class="form-control mb-4 col-4" placeholder="Last name">
<div class="form-group blu-margin">
<select class="form-control" th:field="${thepublisher}" id="dropOperator">
<option value="0">select operator</option>
<option th:each="publisher : ${thepublisher}" th:value="${publisher.id}" th:text="${publisher.publisherName}"></option>
</select>
</div>
<button type="submit" class="btn btn-info col-2">Save</button>
</form>
@Id
@GeneratedValue(strategy= GenerationType.IDENTITY)
@Column(name="id")
private int id;
@Column(name="book_name")
private String bookName;
@Column(name="book_sub_name")
private String bookSubName;
@Column(name="book_series_name")
private String bookSeriesName;
@Column(name="book_isbn")
private String bookISBN;
@Column(name="book_description")
private String bookDescription;
@OneToOne
private Writer theWriter;
@OneToOne
private Publisher thePublisher;
我的出版者的代码如下(我不打算让writer简单化,我也会将其与book对象关联):
我前面提到的添加页面的控制器代码
@GetMapping("/showFormForAdd")
public String showFormForAdd(Model theModel) {
// create model attribute to bind form data
BookDto theBook = new BookDto();
List<Publisher> thePublisher = publisherService.findAll();
theModel.addAttribute("book", theBook);
theModel.addAttribute("thepublisher",thePublisher);
return "book/book-form";
}
我的主要问题是Publisher对象在传递到saveEmployee函数时始终为空。
我可以用出版商填满这个页面,这样就可以把我的价值观传递到那里
@GetMapping("/showFormForAdd")
public String showFormForAdd(Model theModel) {
// create model attribute to bind form data
BookDto theBook = new BookDto();
List<Publisher> thePublisher = publisherService.findAll();
theModel.addAttribute("book", theBook);
theModel.addAttribute("thepublisher",thePublisher);
return "book/book-form";
}
@PostMapping("/save")
public String saveEmployee(@ModelAttribute("book") BookDto theBook) {
// save the employee
Book NewItem = new Book();
NewItem.setBookDescription(theBook.getBookDescription());
NewItem.setBookName(theBook.getBookName());
// use a redirect to prevent duplicate submissions
return "redirect:/book/list";
}