Spring boot 如何将额外参数从html传递到控制器
弹簧靴2.5,百里香 我需要在单击提交时传递对象产品和其他额外参数(数量) html模板:Spring boot 如何将额外参数从html传递到控制器,spring-boot,thymeleaf,Spring Boot,Thymeleaf,弹簧靴2.5,百里香 我需要在单击提交时传递对象产品和其他额外参数(数量) html模板: <!DOCTYPE html> <html xmlns:th="http://www.thymeleaf.org"> <head> <title th:text="${appName}">Category template title</title> <link th:href="@{/public/style.css}"
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<title th:text="${appName}">Category template title</title>
<link th:href="@{/public/style.css}" rel="stylesheet"/>
<meta charset="UTF-8"/>
</head>
<body>
<div class="container">
<h3 th:text="*{title}"/>
<form method="post" action="#" th:object="${product}" th:action="@{/product}">
<input type="hidden" id="id" th:field="*{id}"/>
<input type="text" placeholder="Name" id="name" th:field="*{name}" th:disabled="${isView}"/>
<input type="hidden" id="created" th:field="*{created}"/>
<textarea placeholder="Description" rows="5" id="description"
th:field="*{description}" th:disabled="${isView}"></textarea>
<input type="number" placeholder="Price" id="price" th:field="*{price}" th:disabled="${isView}"/>
<input type="text" placeholder="Currency" id="currency" th:field="*{currency}" th:disabled="${isView}"/>
<input type="text" placeholder="Images URL(separate by comma)" id="images" th:field="*{images}" th:disabled="${isView}"/>
<input th:type="${isView} ? hidden : submit" value="Submit"/>
</form>
</div>
</body>
</html>
因此,当单击按钮Submit时,使用fill object Product调用submitProduct。但是我需要传递额外的参数(作为方法submitProduct
中的第二个参数)-数量。
如何将这个额外的int参数从html传递到控制器?一个选项是直接从请求参数访问值 假设该数量值在表单中作为输入字段可用,名称为
数量
(看起来它目前不在),则您可以更改控制器以使用此字段:
import org.springframework.web.bind.annotation.RequestParam;
然后将相关的方法签名更改为如下内容:
public String submitProduct(Product product, Model model,
@RequestParam(name = "quantity") String quantity) {...}
(我认为还需要某种形式的现场验证。)
public String submitProduct(Product product, Model model,
@RequestParam(name = "quantity") String quantity) {...}