创建一个新的用户帐户Spring数据休息框架

时间:2019-04-07 22:09:51

标签: spring-mvc spring-data-jpa spring-data-rest

我一直在尝试为我的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
}

UserRepository.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>

1 个答案:

答案 0 :(得分:0)

我不知道百里香叶,但是回答您的问题是还可以。

您无需担心视图中的id,它是由dao框架自动创建的。由@GeneratedValue(strategy= GenerationType.AUTO)

您可以使用这种东西,因为它是Spring MVC

<form:hidden path="id"/>