我一直在尝试为我的webApp创建一个新用户。我正在使用spring-data-rest。在前端,潜在用户提交他的信息,然后应用程序应该创建后端信息。代码如下。
user.Java
public class User {
@Id
@GeneratedValue(strategy= GenerationType.AUTO)
private Long id;
private String name;
private String email;
private String password;
private String address;
//getters and setters
}
user repository.Java
@RepositoryRestResource(collectionResourceRel = "user", path = "user")
public interface UserRepository extends PagingAndSortingRepository<User, Long>{
}
是否可以查看以下内容?我需要担心身份证吗?它应该由后端生成,因此将id字段放在视图中没有任何意义。
registration.html
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="https://www.thymeleaf.org" xmlns:sec="https://www.thymeleaf.org/thymeleaf-extras-springsecurity3">
<head>
</head>
<body>
<h3>Welcome, Enter The User Details</h3>
<form:form action="#" th:action="@{/user}" th:object="${user}" method="post">
<div><label> User Name : <input type="text" th:field="*{username}"> </label></div>
<div><label> Password: <input type="password" th:field="*{password}"/> </label></div>
<div><label> Email: <input type="email" th:field="*{email}"/> </label></div>
<div><label> Address: <input type="text" th:field="*{address}"/> </label></div>
<div><input type="submit" value="Submit"/></div>
</form:form>
</body>
</html>
我不知道百里香,但回答你的问题是没关系。
您无需担心视图中的id,它是由您的dao框架自动创建的。通过@GeneratedValue(strategy= GenerationType.AUTO)
你可以使用这种东西,因为它是一个Spring MVC
<form:hidden path="id"/>