これは私のビューコードです。コントローラから渡された値でドロップダウンコンポーネントを設定します。ドロップダウンの選択された値がthymeleaf + spring mvcを使用してコントローラに渡されない
<form class="form-horizontal" th:action="@{/product2}" method="post">
<select th:field="*{product3}" th:remove="all-but-first">
<option th:each="productItem : ${productItems}"
th:value="${productItem.productId}" th:text="${productItem.description}">Product 1</option>
<option value="">Product 2</option>
<option value="">Product 3</option>
</select>
<button type="submit" value="Submit" title="Submit"></button>
</form>
これは私のコントローラで、ドロップダウンの値を入力し、ドロップダウンでフォーム印刷の選択値を送信します。
@Controller
public class IndexController {
@RequestMapping("/")
public ModelAndView index() {
ModelAndView model = new ModelAndView("index");
Product wildFire = new Product();
wildFire.setProductId("WF-458");
wildFire.setDescription("WildFire");
Product weapon = new Product();
weapon.setProductId("WE-81");
weapon.setDescription("Weapon");
List<Product> productItems = new ArrayList<Product>();
productItems.add(weapon);
productItems.add(wildFire);
model.addObject("productItems", productItems);
model.addObject("product3", new Product());
return model;
}
@RequestMapping(value="/product2", method=RequestMethod.POST)
public String showProduct(@ModelAttribute(value="product3") Product product, ModelMap map) {
System.out.println(product.getDescription());
return "products";
}
}
これは商品のデータクラスです。ちょうど参照
public class Product {
private String productId;
private String description;
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public String getProductId() {
return productId;
}
public void setProductId(String productId) {
this.productId = productId;
}
@Override
public String toString() {
return "Product [id=" + id + ", version=" + version + ", productId=" + productId + ", description="
+ description + ", imageUrl=" + imageUrl + ", price=" + price + "]";
}
}
問題ため:コントローラ側(製品ドロップダウンの)選択された値を取得していない、このフォームを送信する際に、様々なソースから考え出しそのコンポーネントは、構文を使用して、オブジェクトと結合しますth:field = "* {object}"。この問題を解決するために私を助けてください。
おかげトン:-)、それ実際に私を助け、働いた、参考のためだけに - 文字列からMyTypeへの変換のために(この場合の製品)私はアイマン・アル・アブシによって提案されたソリューションをhttp://stackoverflow.com/questions/25234357/select-tagオブジェクト付きサイメル付きaf-and-spring-mvc。 – iknownothing