简体   繁体   English

在春季从组合框获取价值

[英]Get value from combobox in spring

I am struggling to find a way to receive the selected combobox value in Spring. 我正在努力寻找一种在Spring中接收选定组合框值的方法。

This is the html form: 这是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>

After the button is clicked, this methed is executed in the controller: 单击按钮后,该方法在控制器中执行:

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

...

return "redirect:/cart";
}

How can i receive the selected value from the combobox in that method? 我该如何从组合框中接收所选值?

Change your select element like this : 像这样更改您的选择元素:

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

And add controller @RequestParam : 并添加控制器@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";
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM