我的百里香叶形式没有拿起输入longUrl。当我在调试中运行该应用程序时,它会在下面用“”打出帖子请求。我如何使它获取表单输入并将其发送到正文中?
胸腺叶形式:
<!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>
即使当我尝试手动填充值时,它仍然没有将其添加到控制器中
<input type="text" name="longUrl" value="somelongurl"/>
控制器代码:
@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}