Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

Java Spring with Hibernate File Uploads and Entity Updates in Spring + Hibernate Updating GIFs

Binyamin Friedman
Binyamin Friedman
14,615 Points

If model contains gif

In the /upload method of the Gif Controller...

@RequestMapping("/upload") public String formNewGif(Model model) { if(!model.containsAttribute("gif")) { model.addAttribute("gif", new Gif()); } model.addAttribute("categories", categoryService.findAll()); model.addAttribute("title", "Upload"); model.addAttribute("action", "/gifs"); model.addAttribute("submit", "Upload!"); return "gif/form"; }

...Chris wrote an if statement I don't understand what the point of it is. How could the model already contain a gif?

Hi Binyamin,

I believe the if statement is there in case the form validation fails. It is there so the user doesn't have to repopulate all the values in the form. So in the case of a failed Gif upload, the previous input field "description" and the previously selected category will survive the redirect.

Just go back to the video "Re-populating Data when Validation Fails" where it is explained around the 4 min mark.

Hope this helps

Binyamin Friedman
Binyamin Friedman
14,615 Points

This question was asked a month ago so I am way past that part of the track, but thank you anyways!