My controller
@RequestMapping(value = "/user/{id}", method = RequestMethod.GET)
public String updateUserById(@PathVariable("id") Long id, Model model) {
User user = userRepository.findOne(id);
model.addAttribute(user);
return "admin/editUser";
}
@RequestMapping(value = "/user/{id}", method = RequestMethod.POST)
@ResponseBody
public String updateUserById(@PathVariable("id") Long id, @ModelAttribute User user) {
userRepository.updateUser(id, user); // with a try catch
}
The dao
@Override
public void updateUser(Long id, User user) {
User userDB = userRepository.findOne(id);
userDB.setFirstName(user.getFirstName());
userDB.setLastName(user.getLastName());
userDB.setEmail(user.getEmail());
userDB.setUsername(user.getUsername());
userRepository.save(userDB);
}
This method works but it's pretty ugly for me. Let's say that the user have just changed the firstname field in the view, how can I adapt my code to only call the function to set the firstname ?
Something like the Observer pattern to notify field that have change ?