Issue
Entity class
@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;
}
This is my view
<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
@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);
}
}
Hello im looking for method to update user with jpa repository.. I ve tried many ways, i browsed trough internet in 1/2 cases it was recieving error id is not present( like with this one way
public User update(User user){
return userRepository.save(user);
}
), or it was not updating user, but creating a new one.. Also im using PasswordEncoder, and when i want to print user password in password field it is just empty, is it possible to type *** like password in field? Thanks for help
Solution
You code has various errors and misunderstandings:
Change it to be something like:
@Controller
public class UserController {
@Autowired
private UserServices userServices;
@GetMapping("/admin/userlist")
public String userList(Model model,@RequestParam(defaultValue = "") String email){
model.addAttribute("users",userServices.findByEmailLike(email));
return "/admin/userlist";
}
@GetMapping("/admin/useractions")
public String userActions(){
return "/admin/useractions";
}
@PostMapping("/admin/useractions")
public String updateUser(@ModelAttribute User user) {
userServices.update(user);
return "/theNextView";
}
@ModelAttribute("user")
public User getUser(@RequestParam(value = "id", required = false) Integer id) {
return id !=null ? userServices.findOne(id) : new User();
}
}
and in your edit form add:
<input type="hidden" name="id" value="${user.id}" />
on both GET and POST getUser(...)
will populate the model with an existing user if the requestParam id
is set as an URL param or a hidden field.
In the latter case, this existing user will be passed to the POST handler and the submitted form data will be bound to this existing instance.
Answered By - Alan Hay
Answer Checked By - Mildred Charles (JavaFixing Admin)