MVC spring database image display

In the context of the MVC application (using Spring MVC in this case), given that I have some image paths stored in the database. How to display them in a view?

Is it the task of dispatchers to download them, and then pass them to the view? If so, how is this done in Spring MVC using JSP?

thanks

+4
source share
2 answers

It may be the controller’s task to pass them as part of the model into the view, but I think it’s a service level task for image processing.

UPDATE: Juregen Hoeller shows you how to do this using annotated controllers here .

+3
source

It really depends on how the actual images are acquired and what they are used for.

If you create a site for sharing photos, for example, where the images are first-class citizens, I completely agree with duffymo's answer - they should be loaded with a service layer and be part of the model.

If, on the other hand, the images in question are purely presentation in nature and are available as public images in your web application (for example, you can display the status of a process as red / yellow / green light), you can display them directly in the field of view. Of course, in this scenario, it’s rather strange to store the actual image paths in the database - I would prefer the image path to be stored somewhere in the resource set.

Using the latter approach, presumably the image paths that you have with respect to some "base" folder and the full path to the image will look like ${prefix}/${base}/${imagePath} :

  • prefix , depending on the deployment configuration, can be explicitly specified somewhere in the configuration (for example, if you have a dedicated image server) OR be as simple as /context , where context is the path where your webapp is deployed (if everything is served from one simple server, without load balancing, etc.)
  • base will be taken from the configuration (or may be empty)
  • imagePath is what you get from the model.

Rendering the image path directly through the JSP should be trivial using the approach described above.

+2
source

All Articles