簡體   English   中英

在春季從組合框獲取價值

[英]Get value from combobox in spring

我正在努力尋找一種在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";
}

我該如何從組合框中接收所選值?

像這樣更改您的選擇元素:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM