如何将百里香注册表格数据存储在数据库中



我使用thymelaf创建了一个注册页面,并希望存储用户在数据库中输入数据。我正确地定义了一切,但当我点击我的注册按钮,它无法存储数据,并且没有错误当我查看注册页面时,显示在控制台中。

文件SignUp.html

<div class="container mt-5">
<div class="card" style="width: 55rem; ">
<div class="card-header text-center bg-info ">
<h3>Register</h3>
</div>
<div class="card-body">
<form class="mx-1 mx-md-4" th:action="@{/save}" th:method="post" th:object="${consumer}">
<div class="mb-3">
<label class="form-label" for="firstName">first Name </label>
<input class="form-control" id="firstName" placeholder="name@example.com" th:field="*{firstName}"
th:type="text">
</div>
<div class="mb-3">
<label class="form-label" for="phoneNumber">phone Number</label>
<input class="form-control" id="phoneNumber" placeholder="name@example.com" th:field="*{phoneNumber}"
th:type="text">
</div>
<div class="mb-3">
<label class="form-label" for="email">Email address</label>
<input class="form-control" id="email" placeholder="name@example.com" th:field="*{email}"
th:type="email">
</div>
<div class="form-check d-flex justify-content-center mb-5">
<input
class="form-check-input me-2"
id="form2Example3c"
type="checkbox"
value=""
/>
<label class="form-check-label">
I agree all statements in <a th:href="@{/term}">Terms of service</a>
</label>
</div>
<div class="d-flex justify-content-center mx-4 mb-3 mb-lg-4">
<button class="btn btn-primary btn-lg" type="button">Register</button>
</div>
</form>
</div>
</div>
</div>

RegisterController类

@PostMapping("/save")
fun getConsumer(@ModelAttribute("consumer") consumer: Consumer, model:Model):String {
consumerRepository.save(consumer)
return "successPage"
}

GetMapping用于注册

@GetMapping("/register")
fun registerPage(@ModelAttribute consumer: Consumer, model: Model): String {
model.addAttribute("consumer", consumer)
return "SignUp"
}

EntityClass

package com.nilmani.testthymeleaf.entity
import javax.persistence.Entity
import javax.persistence.GeneratedValue
import javax.persistence.GenerationType
import javax.persistence.Id
@Entity
data class Consumer(
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
val id:Long = -1,
val firstName:String = "",
val phoneNumber:Long = -1,
val email:String = ""
)

代码没有存储在数据库中的原因是什么?

表单未提交,因为您使用的是一个简单的<button>元素,除非您使用某些JavaScript提交表单,否则单击它将无效。

但是,您可以简单地使用submit:类型的实际<input>元素

<div class="d-flex justify-content-center mx-4 mb-3 mb-lg-4">                            
<input class="btn btn-primary btn-lg" type="submit" value="Register" />
</div>

点击此元素现在将实际提交表单。