【问题标题】:dropdown's selected value not passed to controller using thymeleaf + spring mvc下拉选择的值未使用 thymeleaf + spring mvc 传递给控制器
【发布时间】:2016-05-04 20:02:04
【问题描述】:

这是我的视图代码。用于使用从控制器传递的值填充下拉组件。

<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="*{ 绑定到对象目的}”。请帮我解决这个问题。

【问题讨论】:

    标签: spring-mvc thymeleaf


    【解决方案1】:

    您需要在表单中设置th:object,否则您无法通过这种方式访问​​发布的值。您还必须创建表单的支持 bean。像这样:

    private class ProductForm implements Serializable {
        ...
        private Product product3;
        ...    
    }
    

    您的表单将包含th:object 类型为ProductForm。注意Spring 不明白你传递给product3 的对象是什么类型的,所以你还必须provide a converter for it

    <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>
    

    控制器代码:

    @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 ProductForm());
    return model;
    }
    
    @RequestMapping(value="/product2", method=RequestMethod.POST)
    public String showProduct(@Valid @ModelAttribute(value="productForm") ProductForm productForm, BindingResult bindingResult, ModelMap map) {        
    return "products";
    }
    }
    

    【讨论】:

    • 非常感谢 :-),它实际上帮助了我并且有效,竖起大拇指,仅供参考 - 对于从字符串到 MyType 的转换(在这种情况下为产品),我使用了 Ayman Al-Absi 建议的解决方案在stackoverflow.com/questions/25234357/….
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-01-31
    • 1970-01-01
    • 2017-01-28
    • 1970-01-01
    • 2022-01-17
    • 2018-01-09
    • 2020-01-22
    相关资源
    最近更新 更多