@RequestMapping(value = "/test") public String home7(ModelMap model) { model.addAttrubute("user", new User()); model.addAttrubute("test", new Test()); return "test"; } @RequestMapping(value = "/test", method = RequestMethod.POST) public String home71(ModelMap model, @ModelAttribute("user") User user, @ModelAttribute("test") Test test) { return "welcome"; } 

When sending a POST request, the user and test are reset to zero for some reason. Probably, I just don’t pass them on the form. (Because I don’t know how) Or maybe the approach is wrong. So, how can I pass user and test post objects with a request?

Please help or poke at least in the right direction to find a solution. thanks in advance

    1 answer 1

    I suspect that @ModelAttribute can be only one @ModelAttribute .

    Let's assume there can be two of them. The form has a name field, the User and Test classes also contain such a field. Question: to which class does the field in the form belong? Most likely, in order to avoid such confusion, Spring supports exactly one object annotated with @ModelAttribute .

    I propose to create another class that combines User and Test . Then it should work.

    • Thanks for the great answer. My problem was rather that I did not understand the stupidity of my ideas and in the limited knowledge. thank you so much for the answer - user200192