下拉列表的选定值未使用百日咳+弹簧mvc传递给控制器

时间:2016-05-04 09:30:02

标签: spring-mvc thymeleaf

这是我的观看代码。用于从控制器传递值来填充下拉组件。

<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";
}
}

这是Product的Data类。仅供参考

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 + "]";
}

}

问题:提交此表单时,未获取控制器端的选定值(产品下拉列表),从各种来源发现该组件使用语法绑定到对象:field =&#34 ; * {对象}&#34 ;.请帮我解决这个问题。

1 个答案:

答案 0 :(得分:2)

在您的表单中,您需要设置import pickle class ContextAwarePickler(pickle.Pickler): def persistent_id(self, obj): # if this is a context, return the key if isinstance(obj, Context): return ("Context", context.key) # pickle as usual return None class ContextAwareUnpickler(pickle.Unpickler): def recover_context(self, key_id): ... def persistent_load(self, pid): type_tag, key_id = pid if type_tag == "Context": return self.recover_context(key_id) else: raise pickle.UnpicklingError("unsupported persistent object") ,否则您无法以这种方式访问​​发布的值。您还必须创建表单的辅助bean。像这样:

th:object

您的表单将private class ProductForm implements Serializable { ... private Product product3; ... } 类型为th:object。请注意,ProductForm不了解您传递给Spring的对象类型,因此您还必须provide a converter for it

product3

控制器代码:

<form th:object="${productForm}" 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>