Класс сущности
@Entity
public class User {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer id;
@NotNull
private String firstName;
@NotNull
private String lastName;
@NotNull
@Email
private String email;
private String country;
private String state;
private String roles;
private String permissions;
@NotNull
@Size(min=6)
private String password;
private String telNumber;
public User() {
}
public User(String firstName,String lastName,String email,String roles,String permissions,String password) {
this.firstName=firstName;
this.lastName=lastName;
this.email=email;
this.roles=roles;
this.permissions=permissions;
this.password=password;
}
public String getCountry() {
return country;
}
public void setCountry(String country) {
this.country= country;
}
public String getState() {
return state;
}
public void setMesto(String state) {
this.state= state;
}
public String getRoles() {
return roles;
}
public List<String> getRolesList(){
if(this.roles.length()>0){
return Arrays.asList(this.roles.split(","));
}
return new ArrayList<>();
}
public List<String> getPermissionsLisst(){
if(this.permissions.length()>0){
return Arrays.asList(this.permissions.split(","));
}
return new ArrayList<>();
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public void setRoles(String roles) {
this.roles = roles;
}
public void setPermissions(String permissions) {
this.permissions = permissions;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getTelNumber() {
return telNumber;
}
public void setTelNumber(String telNumber) {
this.telNumber = telNumber;
}
Это мой взгляд
<div class="udaje container mx-auto text-dark">
<form th:action="@{/admin/useractions}" th:object="${user}" method="post">
<div class="form-group">
<label for="firstName" class="form-control-label">First Name</label> <input
type="text" class="form-control" th:field="*{firstName}" id="firstName" />
<div class="text text-danger" th:if="${#fields.hasErrors('firstName')}"
th:errors="*{firstName}"></div>
</div>
<div class="form-group">
<label for="lastName" class="form-control-label">Last Name</label> <input
type="text" class="form-control" th:field="*{lastName}" id="lastName" />
<div class="text text-danger" th:if="${#fields.hasErrors('lastName')}"
th:errors="*{lastName}"></div>
</div>
<div class="form-group">
<label for="email" class="form-control-label">Email</label> <input
type="text" class="form-control" th:field="*{email}" id="email" />
<div class="text text-danger" th:if="${#fields.hasErrors('email')}"
th:errors="*{email}"></div>
</div>
<div class="form-group">
<label class="form-control-label">State</label><input
type="text" class="form-control" th:field="*{state}" id="state" />
<div class="text text-danger" th:if="${#fields.hasErrors('state')}"
th:errors="*{state}"></div>
</div>
<div class="form-group">
<label for="country" class="form-control-label">Country</label> <input
type="text" class="form-control" th:field="*{mesto}" id="country" />
<div class="text text-danger" th:if="${#fields.hasErrors('country')}"
th:errors="*{country}"></div>
</div>
<div class="form-group">
<label for="password" class="form-control-label">Password</label> <input
type="password" class="form-control" th:field="*{password}" id="password" />
<div class="text text-danger" th:if="${#fields.hasErrors('password')}"
th:errors="*{password}"></div>
</div>
<div class="text-center">
<input type="submit" value="Submit" class="btn btn-dark" />
</div>
</form>
Контроллер
@Controller
public class UserController {
@Autowired
private UserServices userServices;
private UserRepository userRepository;
public int roh;
@GetMapping("/admin/userlist")
public String userList(Model model,@RequestParam(defaultValue = "") String email){
model.addAttribute("users",userServices.findByEmailLike(email));
return "/admin/userlist";
}
@RequestMapping("/admin/useractions")
public String userActions(@RequestParam(value = "id", required = true) Integer id, Model model){
model.addAttribute("user",userServices.findOne(id));
return "/admin/useractions";
}
@PutMapping("/admin/useractions")
public User updateUser(@RequestBody User user) {
return userServices.update(user);
}
}
Здравствуйте, я ищу способ обновить пользователя с помощью репозитория jpa. Я пробовал много способов, я просмотрел через inte rnet в 1/2 случаев, когда он получал идентификатор ошибки, которого нет (как с этим способ
public User update(User user){
return userRepository.save(user);
}
), или он не обновлял пользователя, а создавал нового. Также я использую PasswordEncoder, и когда я хочу напечатать пароль пользователя в поле пароля, он просто пуст, возможно ли это набрать *** как пароль в поле? Спасибо за помощь