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

Sergei Makarov
Sergei Makarov
12,718 Points

Representing bytes[] into the input element by editing GIF-file ?

Hi there,

Is there any possibility to represent bytes[]/name of the already uploaded file in the editing form. Maybe I just want to edit Category or Description for this very file. How it could be made?

By adding th:field ="*{bytes}" into the input form field doesn't make a lot of sense.

Any suggestions?

3 Answers

Alexander Nikiforov
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Alexander Nikiforov
Java Web Development Techdegree Graduate 22,161 Points

Why do you need to represent bytes field in thymeleaf? You don't need to put all of the member variables of the object for the form to get through.

If you want to edit Gif, you get all "good" changed data, like 'name', 'description'. And then before you save Gif, you take the old image of it, and put it to the Gif you are trying to update?

Does it make sense? Sometimes you can have situations where you want to change only specific property of object, like name, and there is no need to put all the rest of the data to thymeleaf. In this case you put to thymeleaf template only id and name, leaving the rest of the fields to be null.

Then All you do is take old Object from database, change his name to the new one, and update it. Without carrying around information of all his fields, but just taking the data from database.

That is if I understood your question correctly...

Sergei Makarov
Sergei Makarov
12,718 Points

This is exactly the problem!

Try to edit you Gif without changing anything but, for example, description, and you will see, that your Gif will be no more shown/persisted. That's the point: it will be set to NULL and NULL will be saved in DB (excepting you have a validation @NotNull on your Gif-Entity).

Either you have to write a SQL-logic in your GifService for update method or you have to check all fields again! That's the point. I want the file would be persisted (loaded) in my <input type="file"> field as all other are.

Alexander Nikiforov
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Alexander Nikiforov
Java Web Development Techdegree Graduate 22,161 Points

Let me show workaround how to change all the fields except image:

  1. Add update(Gif gif) method on Service Layer, like this:
    @Override
    public void update(Gif gif) {
        Gif oldGif = gifDao.findById(gif.getId());
        gif.setBytes(oldGif.getBytes());
        gifDao.save(gif);
    }
  1. Add the following to the GifController.updateGif method:
    @RequestMapping(value = "/gifs/{gifId}", method = RequestMethod.POST)
    public String updateGif(Gif gif,
                            @RequestParam MultipartFile file,
                            RedirectAttributes redirectAttributes) {
        // TODO: Update GIF if data is valid
        if (!file.isEmpty()) {
            gifService.save(gif, file);
        } else {
            System.out.println("file is empty");
            gifService.update(gif);
        }

This way we can use old image if file is empty and update Gif, and put new one if new image was uploaded. Coming back to the problem how to show existing image in input I don't know. May be Chris knows, may be Google... Have to dig more

Alexander Nikiforov
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Alexander Nikiforov
Java Web Development Techdegree Graduate 22,161 Points

Hi. I found this article, take a look:

http://stackoverflow.com/questions/1696877/how-to-set-a-value-to-a-file-input-in-html

I think we cannot set value of file to input theoretically not just in thymeleaf, due to security reasons. So that no one can change the value, otherwise they can steal from you, I guess. Which means we cannot pre-populate file input with value. I like answer with 25 votes:

Not an answer to your question (which others have answered), but if you want to have some edit functionality of an uploaded file field, what you probably want to do is:

  • show the current value of this field by just printing the filename or URL, a clickable link to download it, or if it's an image: just show it, possibly as thumbnail
  • the <input> tag to upload a new file
  • a checkbox that, when checked, deletes the currently uploaded file. note that there's no way to upload an 'empty' file, so you need something like this to clear out the field's value