Spring从html select中获取数据

时间:2015-08-13 15:35:27

标签: spring spring-mvc

我正在使用Spring MVC来构建网站。 我在HTML中有一个简单的表单:

 <form action="#" th:action="@{/new_product}" th:object="${webProduct}" method="post">

            <p>Name: <input type="text" th:field="*{name}" /></p>
            <p>Des: <input type="text" th:field="*{description}" /></p>
            <p>Price: <input type="text" th:field="*{price}" /></p>

            <p>Category: </p>

            <select>
                <th:forEach th:each="category : ${categories}">
                    <option th:text="${category.name}"  th:value="${category.category_id}"></option>
                </th:forEach>
            </select>

            <p><input type="submit" value="Save" /></p>
        </form>

当用户点击按钮时,我调用方法:

@RequestMapping(value = "/new_product", method = RequestMethod.POST)
    public String setNewProduct(@Valid WebProduct product, Model model){
        //productService.add(product, "test");

        System.out.println("Value: " + product.getName());
        System.out.println("Value: " + product.getDescription());
        System.out.println("Value: " + product.getPrice());
        System.out.println("Value: " + product.getCategoryId());

        return "add_ok";
    }

Namedescriptionprice正在展示,但categoryId未展示。问题出在哪儿?

0 个答案:

没有答案