Stack searched but did not find the answer. I have subpages with several textfield's and when I editing the data field a plugin sends a reply to my controller:
@RequestMapping(value = "/myAcc", method = RequestMethod.POST)
public String getValues(@ModelAttribute XEditableForm form){
userService.update(form.getPk(), form.getValue());
return "myAcc";
}
where userService make a user update in db
@Transactional
public void update(long id, String firstName){
User user= userRepository.findOne(id);
user.setFirstName(firstName);
userRepository.save(user);
}
The problem is that every time in this method I would check what was returned from xeditable plugin and update specific user field i.e. surname etc. In my opinion this is not the best solution.
XEditableForm returns:
pk - primary key of record to be updated (ID in db)
name - name of field to be updated (column in db)
value - new value
Question for you. How can I do this better?
I will answer for himself. In this situation we could use
org.springframework.utilClass ReflectionUtils
Exmaple of use:
User user = userRepository.findOne(id);
Field name = ReflectionUtils.findField(User.class, "name");
ReflectionUtils.makeAccessible(true);
name.set(user, "Admin");
In this way, you can edit the fields in the class knowing their names . And that's what I mean.