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

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

问题描述:

我很努力地找到一种方法来在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

你有没有尝试使用 –

更改您的选择元素:

<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

这仍然给我同样的错误。 –