作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有一个 spring-boot 应用程序。我需要的完整网址:localhost:8080/company/{companyName}/users?name={name}。一开始我选择公司,例如。 :本地主机:8080/公司/谷歌。 Controller 将我重定向到表单 (company.html) 的页面,我在其中输入名称。 Controller :
@GetMapping("/company/{company}")
public String greetingForm(@PathVariable String company, Model model) {
Data data = new Data();
data.setCompany(company);
model.addAttribute("data", data);
return "company";
}
在数据类中,我只存储公司和名称;
我的表单,我在其中输入名称:
<form action="#" th:action="@{/users}" th:object="${data}" method="get">
<p>Name: <input type="text" th:field="*{name}" /></p>
<p><input type="text" th:value="${data.company}"></p>
<p><input type="submit" value="Submit" /> <input type="reset" value="Reset" /></p>
</form>
所以我提交后,结果url是localhost:8080/users?name=Example,我需要localhost:8080/company/google/users?name=Example。我怎样才能改变它?我尝试了 th:action="@{/${data.company}/users}",但 ${data.company} 按字面解释
最佳答案
为什么要同时使用路径参数和查询参数?您可以仅使用路径参数或仅使用查询参数来完成此操作。
作为您问题的答案,您可以尝试以下操作:
<form th:action="@{/company/{id}/users(id=${company.name})}" method="get">
<input type="hidden" name="name" th:value="${user.name}">
<input type="submit" value="Submit" />
</form>
另一种选择:
<form th:action="@{/company/{id}/users(id=${company.name},name=${user.name})}" method="get">
<input type="submit" value="Submit" />
</form>
有多种方法可以将您的请求正确发送到 Controller 。
<form th:action="@{/service}" method="get">
<input type="text" name="company" th:value="${company.name}" />
<input type="text" name="name" th:value="${user.name}" />
<button type="submit">Submit</button>
</form>
输出为:'..../service?company=google&name=david'
<a th:href="@{...}">
<span>Submit</span>
</a>
关于java - 我如何向 :action form 添加数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61878407/
我是一名优秀的程序员,十分优秀!