我的 thymeleaf 表单没有获取输入的 longUrl。当我在调试时运行应用程序时,它会使用“”命中下面的发布请求。我怎样才能让它获取表单输入并将其发送到正文中?
thymeleaf 形式:
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.w3.org/1999/xhtml">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<form method="POST" th:action="@{create_short_url_thymeleaf}">
<h1>Hit Enter to Shorten Your Long Url</h1>
<input type="text" th:name="longUrl"/>
</form>
</body>
</html>
即使我尝试手动填充该值,它仍然无法进入 Controller
<input type="text" name="longUrl" value="somelongurl"/>
Controller 代码:
@Controller
@RequestMapping("/api/v1")
public class UrlShorteningController {
@GetMapping("/create_short_url")
public String newShortUrl(Model model) {
model.addAttribute("longUrl",
"");
return "some-form";
}
@PostMapping("/create_short_url_thymeleaf")
ResponseEntity<String> newShortUrlFromThymeleaf(@ModelAttribute String longUrl) {
// Running the application on debug, I make it here, but the longUrl is empty.
....
}
最佳答案
尝试更换
th:action="@{create_short_url_thymeleaf}
与
th:action="@{/create_short_url_thymeleaf}
关于java - Thymeleaf 模板未发布正文,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62475523/