I need to display images which reside outside of deploy folder in web application using JSF <h:graphicimage>
tag or HTML <img>
tag. How can I achieve that?
相关问题
- Views base64 encoded blob in HTML with PHP
- How to get the background from multiple images by
- java.lang.NullPointerException at java.io.PrintWri
- h:selectOneMenu in p:dataTable doesn't submit
- CV2 Image Error: error: (-215:Assertion failed) !s
相关文章
- Use savefig in Python with string and iterative in
- Where does this quality loss on Images come from?
- Specifying image dimensions in HTML vs CSS for pag
- How to insert pictures into each individual bar in
- How do I append metadata to an image in Matlab?
- Img url to dataurl using JavaScript
- Click an image, get coordinates
- C# Saving huge images
In PrimeFaces you can implement your bean in this way:
In your HTML Facelet:
I suggest to set the attribute cache="false" in the graphicImage component.
In JSP
Packages are
com.sun.jersey.core.util.Base64
,java.nio.file.Paths
andjava.nio.file.Files
.In order to achieve what you need using
<h:graphicImage>
or<img>
tags, you require to create a Tomcat v7 alias in order to map the external path to your web app's context.To do so, you will need to specify your web app's context. The easiest would be to define a META-INF/context.xml file with the following content:
Then after restarting your Tomcat server, you can access your images files using
<h:graphicImage
> or<img>
tags as following:or
*Note the context path is necessary for the tag but not for the
Another possible approach if you don't require the images to be available through HTTP GET method, could be to use Primefaces
<p:fileDownload>
tag (using commandLink or commandButton tags - HTTP POST method).In your Facelet:
In your bean:
To the point, it has to be accessible by a public URL. Thus, the
<img src>
must ultimately refer ahttp://
URI, not something like afile://
URI or so. Ultimately, the HTML source is executed at enduser's machine and images are downloaded individually by the webbrowser during parsing the HTML source. When the webbrowser encounters afile://
URI such asC:\path\to\image.png
, then it will look in enduser's own local disk file system for the image instead of the webserver's one. This is obviously not going to work if the webbrowser runs at a physically different machine than the webserver.There are several ways to achieve this:
If you have full control over the images folder, then just drop the folder with all images, e.g.
/images
directly in servletcontainer's deploy folder, such as the/webapps
folder in case of Tomcat and/domains/domain1/applications
folder in case of GlassFish. No further configuration is necessary.Or, add a new webapp context to the server which points to the absolute disk file system location of the folder with those images. How to do that depends on the container used. The below examples assume that images are located in
/path/to/images
and that you'd like to access them via http://.../images.In case of Tomcat, add the following new entry to Tomcat's
/conf/server.xml
inside<Host>
:In case of GlassFish, add the following entry to
/WEB-INF/glassfish-web.xml
:In case of WildFly, add the following entry inside
<host name="default-host">
of/standalone/configuration/standalone.xml
...... and further down in
<handlers>
entry of the very same<subsystem>
as above<location>
:Or, create a
Servlet
which streams the image from disk to response:If you happen to use OmniFaces, then the
FileServlet
may be useful as it also takes into account head, caching and range requests.Or, use OmniFaces
<o:graphicImage>
which supports a bean property returningbyte[]
orInputStream
:Or, use PrimeFaces
<p:graphicImage>
which supports a bean method returning PrimeFaces-specificStreamedContent
.For the first way and the Tomcat and WildFly approaches in second way, the images will be available by http://example.com/images/filename.ext and thus referencable in plain HTML as follows
For the GlassFish approach in second way and the third way, the images will be available by http://example.com/context/images/filename.ext and thus referencable in plain HTML as follows
or in JSF as follows (context path is automatically prepended)
For the OmniFaces approach in fourth way, reference it as follows
For the PrimeFaces approach in fifth way, reference it as follows:
See also: