从春天的组合框中获取价值

时间:2017-10-11 11:32:29

标签: java html spring combobox

我正在努力寻找一种方法来接收Spring中选定的组合框值。

这是html表单:

<form method="post" multiple="true">
    Authhor: <label th:text="*{ownerName}" /><br />
    Title: <label th:text="*{name}" /><br />
    Description: <label th:text="*{description}" /><br />
    Price: €<label th:text="*{price}" /><br />
    Product: <select th:field="${productss}" th:remove="all-but-first">
        <option th:each="product : ${productss}" 
                th:value="${product.id}" th:text="${product.name + '   (+ €' + product.price + ')'}">Productname</option>
    </select><br />
    <img width="250" heigth="250" th:src="*{plainURL}"/><br />
    <button align="right" class="btn" type="submit" name="add" ><span class="glyphicon glyphicon-check">Add to shopping cart</span></button><br />
</form>

单击按钮后,此控制将在控制器中执行:

@RequestMapping(value = IMAGE_ID, method = RequestMethod.POST, params = {"add"})
public String add(@PathVariable("id") int id, HttpSession session, Model model) {

...

return "redirect:/cart";
}

如何从该方法中的组合框中接收所选值?

1 个答案:

答案 0 :(得分:0)

更改您的选择元素:

<select th:field="${productss}" th:remove="all-but-first" name="product" >
        <option th:each="product : ${productss}" 
                th:value="${product.id}" th:text="${product.name + '   (+ €' + product.price + ')'}">Productname</option>
    </select><br />

添加控制器@RequestParam

@RequestMapping(value = IMAGE_ID, method = RequestMethod.POST, params = {"add"})
public String add(@PathVariable("id") int id,@RequestParam Integer product, HttpSession session, Model model) {

...

return "redirect:/cart";
}