在Spring Boot中单击Submit按钮后,结果页面不再显示

时间:2018-08-30 12:41:37

标签: spring spring-mvc spring-boot

我正在尝试通过参考以下网址在Spring Boot中实现一个表单应用程序:

https://spring.io/guides/gs/handling-form-submission/

我能够呈现输入表单页面,然后单击提交按钮。之后,该页面需要呈现到result.html,但不会显示到result.html页面。

请找到以下代码:

@Controller
public class SearchByID {

    @GetMapping("/searchByID")
    public String searchByIDForm(Model model) {

        model.addAttribute("searchByID", new Search());
        return "searchbyID";
    }

    @PostMapping("/searchByID")
    public String searchByIDSubmit(@ModelAttribute Search search) {

        return "result";
    }

}

result.html
==========
<!DOCTYPE HTML>
<html xmlns:th="http://www.thymeleaf.org">
<head>
    <title>GUI Application</title>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
</head>
<body>
    <h1>Result</h1>
    <p th:text="'id: ' + ${search.id}" />
</body>
</html>

searchbyID.html
==============

<!DOCTYPE HTML>
<html xmlns:th="http://www.thymeleaf.org">
<head>
    <title>GUI Application</title>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
</head>
<body>
    <h1>GUI Application</h1>
    <form action="#" th:action="@{/searchByID}" th:object="${searchByID}" method="get">
        <p>ID: <input type="text" th:field="*{id}" /></p>
        <p><input type="submit" value="Search" /> <input type="reset" value="Reset" /></p>
    </form>
</body>
</html>

有人可以帮忙吗?

1 个答案:

答案 0 :(得分:1)

通过将方法从“获取”更改为“发布”来解决问题,如下所示:

form action="#" th:action="@{/searchByID}" th:object="${searchByID}" method="get">
        <p>ID: <input type="text" th:field="*{id}" /></p>
        <p><input type="submit" value="Search" /> <input type="reset" value="Reset" /></p>
    </form>