Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Java Spring with Hibernate User Messages in Spring Re-populating Data when Validation Fails

Anish Palan
Anish Palan
3,157 Points

While passing category attribute using the addFlashAttribute method of RedirectAttribute object, does Model get it?

When you say at 4:10 in this video that we check if Model contains the attribute "category", how does it get that. Is it while passing category attribute using the addFlashAttribute method of RedirectAttribute object that it gets it from there, because when on first call to /categories/add method we have that new category added but as you told while redirect in /categories POST call it would get destroyed.

1 Answer

I know this question is old, but...

My non-expert take is that yes, when from index.html, a user clicks the '+' sign to add a new Category, formNewCategory() is called and a new object is added to the model via:

    model.addAttribute("category",new Category()); 

...then after other attributes are added to the model, user is taken to "category/form" to fill in new Category object properties. When the user adds invalid properties for the new Category, then clicks 'submit', the <form> element in form.html has the action of "/category" (originally added as 'action' variable after if-then statement in formNewCategory()) and that action is mapped to addCategory(). So, in the controller, in addCategory(), program flow enters the if-then due to invalid properties, and the RedirectAttributes object adds the new invalid category object to the model --

        // Add  category if invalid was received
        redirectAttributes.addFlashAttribute("category",category);

My take is that this invalid category object overwrites the previous one.