Reputation: 114
In my web application, I want to upload images and hence present them using p:graphicImage. I store the uploaded images outside the web app – in particular at E:\webapp\upload. Note that my tomcat server is installed on E: volume. I have created an ImageServlet to read the images as described in http://balusc.blogspot.com/2007/04/imageservlet.html.
I added the following configuration to my web.xml file:
<servlet>
<servlet-name>imageServlet</servlet-name>
<servlet-class>utilities.ImageServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>imageServlet</servlet-name>
<url-pattern>/upload/*</url-pattern>
</servlet-mapping>
I can access the images from the index.xhtml page using “upload/imageName.ext” eg:
<p:graphicImage value="upload/flag.gif" />
This jsf code is correctly converted into the following HTML code:
<img src=”http://localhost:8080/MyApp/upload/flag.gif?pfdrid_c=true”>
However, I want to be able to access images from pages which are contained into subfolders. Eg I have one page which is located at “admin” subfolder:
http://localhost:8080/MyApp/admin/language.xhtml
Inside that page, the same jsf code is converted into:
<img src=”http://localhost:8080/MyApp/admin/upload/flag.gif?pfdrid_c=true”>
which is wrong.
In other words, it looks for the images at “E:\webapp\admin\upload” directory which does not exist and hence nothing is displayed. I can clearly write the following code:
<p:graphicImage value="../upload/flag.gif" />
to make it work but I consider this as a bad practice because I will have to adopt the path each time I am in a different subfolder hierarchy (consider for example the problem of nested subfolders).
I am wondering if there is any way to just specify the relative path of the image eg “upload/flag.gif” regardless of subfolder hierarchy.
Thanks in advance!
Upvotes: 1
Views: 1031
Reputation: 1108722
Just let the path start with /
to make it relative to the context root instead of the currently opened folder.
<p:graphicImage value="/upload/flag.gif" />
Upvotes: 1