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 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?

Nelson Fleig
Nelson Fleig
25,764 Points

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!