Spring MVC - how not to lose field values ​​when binding to a form partially

I want to make an update form for bean X. This bean allows me to say that it has fields A, B, C, D. In my form, I want to update only fields A, B and give C and D untouched. Is there a way to bind bean X to the update form using only fields A and B so that when submitting forms C and D are not changed?

I know that I can add hidden fields for C and D, but what if they are not primitive fields, they are other beans or collections.

I know that another solution would be to create an XUpdateBean that will only have fields A and B and after submitting the form, pass the fields from XUpdateBean to my X bean.

Is there any other way for this update better in Spring 3 MVC?

+6
source share
3 answers

You can have a command-Object / form-barking-Bean that contains only the fields you need.

In the controller, you must load the bean X, and you need to update its fields using the commandObject.

Can you also think about not having an extra class for commandObject, use the BeanX class instead. But, of course, you need two BeanX instances: one for commandObject and one for bean x.

+3
source

Sorry, I don't know SpringMVC, so my answer may be wrong.

With another binding structure called Stripes, we usually “hydrate” the data before binding. This means that you first load the bean from db, and only then will you bind the value A and B to it! But it still has the original values ​​of C and D, since the bean comes from the database (usually a JPA object). Thus, you do not need hidden fields C and D!

Seems possible with SpringMVC: Spring MVC 3.0: how do I bind to a persistent object

Note that you can also load the binding to a “non-DB bean” as you actually are (so you will have fields C and D, I think if you do not use hidden fields). Then you can simply load the bean that you want to change from the database and merge between the attached bean and db bean fields you want (here you merge only A and B so that the fields C and D in the DB bean are not changed)

Here you can find interesting material on data binding. For me, in some complex cases, binding directly to database objects can be dangerous: Using the binding infrastructure efficiently

0
source

The correct way, in my opinion, especially when working with Optimistic Concurrency Control (@Version), is to temporarily store the model attribute in the session.

@Controller @SessionAttributes("x") public class MyController { @Autowired private XRepository xRepository; @InitBinder void initBinder(WebDataBinder binder) { binder.setDisallowedFields("id", "c", "d"); } @RequestMapping("/x/{id}") String myForm(@PathVariable("id") long id, Model model) { X x = xRepository.findOne(id); model.addAttribute("x", x); return "x-edit"; } @RequestMapping(value="/x/{id}", method= RequestMethod.POST) String save(@PathVariable("id") long id, @ModelAttribute X x, SessionStatus sessionStatus) { xRepository.save(x); sessionStatus.setComplete(); return "x-edit"; } } 
0
source

Source: https://habr.com/ru/post/909645/


All Articles