的模型我創建了一個表單,它有2個字段(產品名稱和價格)以及類別對象(產品類別)的下拉列表。 我不知道如何使這項工作,當我有一個類別對象設置在產品對象。Spring MVC表單 - 參考對象
產品:
public class Product {
private String name;
private Category category;
private int price;
public Product() {
}
public Product(String name, Category category, int price) {
this.name = name;
this.category = category;
this.price = price;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Category getCategory() {
return category;
}
public void setCategory(Category category) {
this.category = category;
}
public int getPrice() {
return price;
}
public void setPrice(int price) {
this.price = price;
}
}
產品控制器:
@ModelAttribute("categoryList")
public List<Category> categoryList() {
return categoryService.getCategories();
}
@RequestMapping("/products/add")
public ModelAndView addProductForm() {
ModelAndView mv = new ModelAndView("addProduct");
mv.addObject("product", new Product());
return mv;
}
@RequestMapping(value = "/products/add/process", method = RequestMethod.POST)
public ModelAndView addProduct(@ModelAttribute("product") Product product) {
ModelAndView mv = new ModelAndView("products");
System.out.println("added " + product.getName() + " " + product.getPrice());
return mv;
}
形式:
<form class="form-horizontal" action="#"
th:action="@{/products/add/process}" th:object="${product}"
method="post">
<fieldset>
<!-- Form Name -->
<legend>Add product</legend>
<!-- Text input-->
<div class="form-group">
<label class="col-md-4 control-label" for="textinput">Product
name</label>
<div class="col-md-4">
<input id="textinput" name="textinput" placeholder="Product name"
class="form-control input-md" required="" type="text"
th:field="*{name}"></input>
</div>
</div>
<!-- Select Basic -->
<div class="form-group">
<label class="col-md-4 control-label" for="selectbasic">Category</label>
<div class="col-md-4">
<select th:field="*{category}">
<option th:each="cat : ${categoryList}" th:value="${cat.getId()}"
th:text="${cat.getName()}"></option>
</select>
</div>
</div>
<!-- Text input-->
<div class="form-group">
<label class="col-md-4 control-label" for="textinput">Price</label>
<div class="col-md-4">
<input id="textinput" name="textinput" placeholder=""
class="form-control input-md" required="" type="text"
th:field="*{price}"></input>
</div>
</div>
<!-- Button -->
<div class="form-group">
<label class="col-md-4 control-label" for="singlebutton"></label>
<div class="col-md-4">
<button id="singlebutton" name="singlebutton"
class="btn btn-success">Add product</button>
</div>
</div>
</fieldset>
</form>
附加從評論
當我SUBM信息它它(見addProduct方法 - 這是一個表單處理程序)我得到:java.lang.IllegalStateException: Cannot convert value of type [java.lang.String] to required type [com.example.shop.Category] for property 'category': no matching editors or conversion strategy found]
。我簡直無法將字符串從下拉菜單轉換爲對象
所以我明白的問題正確:該類別的下拉是空的,因爲你永遠不會設置它。你如何獲得填充值並返回到表單? – Ascalonian
@Ascalonian我在下拉菜單中顯示它們,我使用ModelAttribute註釋將它們作爲列表傳遞(我使用categoryService獲取它)。 – tomdavies
然後我不明白「如何使這項工作」的意思,因爲你說下拉顯示值。你能更詳細地描述你想要完成的事情嗎? – Ascalonian