2017-10-11 27 views
0

我很努力地找到一種方法來在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"; 
} 

如何從在組合框接收所選擇的值方法?這樣

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

這樣做給我的錯誤:必需產品參數「產品」不存在 –

+0

嘗試更改**整數*到**字符串**產品 – mrtasln

+0

這仍然給我同樣的錯誤。 –