views:

1322

answers:

2

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

Is it the controllers job to load them and then pass them to the view? If so, how is this done in Spring MVC using JSP?

Thanks

+2  A: 

It might be the controller's job to pass them as part of model to the view, but I think it's the service layer's job to deal with acquiring the image.

UPDATE: Juregen Hoeller shows you how to do it with annotated controllers here.

duffymo
Lehane
One way to do it is to store the URL in the database and the image itself on the file system. Or you can put the image in the database as a BLOB. The service would return an Image object and the controller would serialize it appropriately for the view.
duffymo
+1  A: 

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

If you are building a photo-sharing website, for example, where images are first-class citizen s, I fully agree with duffymo's answer - they should be loaded by service layer and be a part of the model.

If, on the other hand, images in question are purely presentational in nature and are available as public images within your web app (for example, you may be displaying status of some process as red / yellow / green light), you can render them directly in view. Of course, in this scenario it's rather strange to keep actual image paths in the database - I would rather imagine image path would be stored somewhere in the resource bundle instead.

Using the latter approach, presumably image paths you have are relative to some "base" folder and full image path would be something like ${prefix}/${base}/${imagePath}:

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

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

ChssPly76
Yes, the images are first class citizens, and so I guess should be obtained in the service layer.
Lehane