java - Thymeleaf 模板未发布正文

标签 java spring-mvc input thymeleaf

我的 thymeleaf 表单没有获取输入的 longUrl。当我在调试时运行应用程序时,它会使用“”命中下面的发布请求。我怎样才能让它获取表单输入并将其发送到正文中?

enter image description here

enter image description here

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/

相关文章:

java - 如何使用java从jasperserver报告生成PDF文件

spring - 如何在 spring mvc Controller 中为一种方法设置多个模式

input - MapReduce 中输入分割的主要原因是什么?

html - 网页中的声音输入

PHP 正则表达式 - 1 个或多个整数的值

java - 如何从 Object 参数获取类?

java - 是否可以使用@Advice.OnMethodEnter 从方法返回?

java - 如何使用 Spring Boot RSocket 服务器支持 WebSocket 传输?

java - Spring xml ...有条件加载变量

java - 通过java从mysql的数据创建json对象