3

I am trying to display image placed in some folder at my machine.I have an xhtml page on which i want to display the image.

I have tried using <h:graphicImage value="C:/images/abc.jpg" /> as well as plain HTML tag <img src="C:/images/abc.jpg" /> but neither of above is working.

Moreover I have tried placing image in WebContent and tried to access it from there and still in vain.where am i going wrong?

I am using Jboss AS 7.0

Sajjad
  • 33
  • 1
  • 4

2 Answers2

4

If you want to access a image outside webContent you need to work a little more. Basically you have three options: you use a servlet, PrimeFaces or OmniFaces. This question was also answered here:

Load images from outside of webapps / webcontext / deploy folder using <h:graphicImage> or <img> tag

I had this same problem in the past and I resolved it by using a servlet. I defined it in this way

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.annotation.WebServlet;

@WebServlet("/images/*")
public class ImageServlet extends HttpServlet {

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    String filename = request.getPathInfo().substring(1);
    File file = new File("C:Images/", filename);
    response.setHeader("Content-Type", getServletContext().getMimeType(filename));
    response.setHeader("Content-Length", String.valueOf(file.length()));
    response.setHeader("Content-Disposition", "inline; filename=\"" + filename + "\"");
    Files.copy(file.toPath(), response.getOutputStream());
}

}

then, if you want to show C:/images/abc.jpg you use

<h:graphicImage value="/images/abc.jpg" />

It worked fine for me.

Community
  • 1
  • 1
hjbello
  • 643
  • 4
  • 18
  • If the question is also answered somewhere else, it is good practice in Stackoverflow to mark the question as a duplicate (under close/flag). – Kukeltje Oct 20 '16 at 15:15
1

You can try this :

http://www.mkyong.com/jsf2/jsf-2-graphicimage-example/

mstzn
  • 2,881
  • 3
  • 25
  • 37
  • I have tried this example and it works fine, but i am unable to understand why is it necessary to put images in resources/images inside the src folder.if i place them somewhere else like in webContent/images,outside of src folder,it is not working. – Sajjad Oct 03 '13 at 17:53
  • 1
    That's because jsf has a default directory for image files. If you place it there, jsf will search for it. By the way, if you do Maven, src is also the default directory for every single resource you need, the root of your app will src/main/webapp. – Aritz Oct 04 '13 at 05:50
  • @XtremeBiker i successfully executed it without placing it in resources folder.The problem was,i was not adding `.xhtml`that is why the image tags in xhtml were not getting parsed. – Sajjad Oct 10 '13 at 08:00