Do I have a database with some images? Did anyone explain to me how I can load an image in JSF page ?
I already have a managed Bean that converts an image object into a stream content. This stream content is tagged in & lt; H: graphicImage & gt;
in the page, but when I check the source code of the page, then any src
can not be loaded.
JSF & lt; H: graphic image & gt;
As an HTML and lt; Img & gt; The
element should its src
attribute point to a URL, not the binary content. You should store the URL (or at least some identifier to request parameters or Pathinofo) in JSF bean and create a separate servicelet to stream the image from the DB into the HTTP response.
Use it for your JSF page:
& lt; H: value of graphic image = "picture / # {bean.imageId}" & gt;
Assume that bean.getImageId ()
return 123
, enter HTML in this form as follows:
& lt; Img src = "Pictures / 123" & gt; A
mapped inservlet
classurl-pattern
toweb.xml
code> / Implement images / * and its doGet ()
method as follows.
long image id = long.valof (request.getPathInfo () .substring (1)); // 123 (PS: Do not forget to handle any exception) image image = image DAO.find (imageId); // get image from db // image class is just a jababeen with the following properties: // personal string filename; // Private long length; // PrivateInstream Content; Response.setHeader ("content-type", getServletContext (). GetMimeType (image.getFilename ())); Response.setHeader ("content-length", image.getLength ()); Response.setHeader ("content-displacement", "inline; filename = \" "+ image.getFilename () +" \ ""); BufferedInputStream input = null; BufferedOutputStream Output = Faucet; Try {input = new BufferedInputStream (image.getContent ()); Output = new BufferedOutputStream (response.getOutputStream ()); Byte [] buffer = new byte [8192]; Integer length; While ((length = input. Read (buffer)) gt; 0) {output.write (buffer, 0, length); }} Finally {try (if output! = Zero) {output.close (); } Hold (IOException logOrIgnore) {} if (input! = Zero) try {input.close (); } Hold (IOException logOrIgnore) {}}
in the ImageDAO # find ()
using the image for you as InputStream
From the database can
A detailed example can be found.
Comments
Post a Comment