Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jsf 2.0 displaying images located on local directory

Tags:

jsf-2

jboss7.x

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

like image 590
Sajjad Avatar asked Oct 17 '25 14:10

Sajjad


2 Answers

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.

like image 193
hjbello Avatar answered Oct 19 '25 11:10

hjbello


You can try this :

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

like image 32
mstzn Avatar answered Oct 19 '25 11:10

mstzn



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!