简体   繁体   中英

Get value from combobox in spring

I am struggling to find a way to receive the selected combobox value in Spring.

This is the html form:

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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