spring mvc避免不提交

z9ju0rcb  于 2021-07-13  发布在  Java
关注(0)|答案(1)|浏览(286)

我刚接触spring,我试过编写一个原型。
我试过做个表格。每当我按下提交按钮,什么都不会发生。我正在使用springboot2.4.3和oracleopenjdk15.0.2。我试过firefox和chrome。js控制台为空。
这是我的模型( Patient.java ):

public class Patient implements Serializable {
    private long id;
    private String firstname;
    private String lastname;

    // Geters and seters
}

我的控制器( PatientController.java ):

@Controller
public class PatientController {
    @GetMapping("/patient")
    public String patientForm(Model model) {
        model.addAttribute("patient", new Patient());
        return "addPatient";
    }

    @PostMapping("/patient")
    public String patientSubmit(@ModelAttribute("patient") Patient patient, Model model) {
        model.addAttribute("patient", patient);
        return "addedPatient";
    }
}

我的 addPatient.html :

<!DOCTYPE html>
<html xmlns:th="https://www.thymeleaf.org" lang="en">
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
    <title>HTL-Testet Prototype</title>
</head>
<body>
    <h1>Add a patient</h1>
    <from action="#" th:action="@{/patient}" th:object="${patient}" method="post">
        <p>Id: <input type="number" th:field="*{id}"/></p>
        <p>Firstname: <input type="text" th:field="*{firstname}"/></p>
        <p>Lastname : <input type="text" th:field="*{lastname}"/></p>
        <button type="submit">Register</button>
    </from>
</body>
</html>

我的 addedPatient.html :

<!DOCTYPE html>
<html xmlns:th="https://www.thymeleaf.org" lang="en">
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
    <title>HTL-Testet Prototype</title>
</head>
<body>
    <h1>Add a patient</h1>
    <p th:text="'Added ' + '[' + ${patient.id} + ']' + ${patient.firstname} + ' ' + ${patient.lastname}"></p>
    <a href="/patient">Add another patient</a>
</body>
</html>
mnemlml8

mnemlml81#

这个 from 标签 addPatient.hmtl 页面错误,如果您将其更改为 form 标签如下,问题解决了:

<!DOCTYPE html>
<html xmlns:th="https://www.thymeleaf.org" lang="en">
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
    <title>HTL-Testet Prototype</title>
</head>
<body>
    <h1>Add a patient</h1>
    <form action="#" th:action="@{/patient}" th:object="${patient}" method="post">
        <p>Id: <input type="number" th:field="*{id}"/></p>
        <p>Firstname: <input type="text" th:field="*{firstname}"/></p>
        <p>Lastname : <input type="text" th:field="*{lastname}"/></p>
        <button type="submit">Register</button>
    </form>
</body>
</html>

相关问题