Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
920 views
in Technique[技术] by (71.8m points)

java - <h:graphicImage> is not getting refreshed in JSF

I would like to display a graphic image in a jsf web application from a xhtml file that's changing for each run. I am displaying an image once the upload is done. But what happening is after uploading any image it always displays the one which I uploaded at first time. On doing refresh it displaying the recent uploaded image. Why it is always displaying an image I uploaded at first try. How could I display the most recent uploaded image with h:graphicImage. I am using servlet to display an image. Can anyone help me out...???

here is my code for displaying an image:

public void displayImage(byte[] image,HttpServletResponse response) { 
      ServletOutputStream out;
      if ( image != null ) {
      BufferedImage bufferedImage; 
           try {
                      bufferedImage = ImageIO.read(new ByteArrayInputStream(image)); 
                out = response.getOutputStream(); 
                ImageIO.write(bufferedImage, "png",out);
           } catch (IOException e) { 
                logger.info("Exception in reading the image" + e);
           }
      } else {     
           logger.info("No bytes present ");    
      }  

this is my actual requirement... In first window I have signature box. In that I have upload button.Once the upload is done then I will show an window(2) with an uploaded image there i can crop the image such that I could save it on the signature box of the first window. it is working fine. But what happens when i cancel/close the 2nd window is the signature box of 1st remains empty. So user can upload some other image. In that case, when I try to upload an image, Its successfully getting uploaded but it is not displayed on 2nd window it still the shows the image which I uploaded at first try. How do i fix it...???

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Tell the browser to not cache the image. Set the following headers on the servlet response before the first bit is ever written to its body:

response.setHeader("Cache-Control", "no-cache, no-store, must-revalidate"); // HTTP 1.1.
response.setHeader("Pragma", "no-cache"); // HTTP 1.0.
response.setDateHeader("Expires", 0); // Proxies.

To prevent hard-nosed browsers from still caching it, add a request parameter with a timestampto the servlet URL.

<h:graphicImage value="imageServlet?id=#{bean.imageId}&amp;t=#{now.time}" />

Where #{now} is been registered in faces-config.xml as follows:

<managed-bean>
    <managed-bean-name>now</managed-bean-name>
    <managed-bean-class>java.util.Date</managed-bean-class>
    <managed-bean-scope>request</managed-bean-scope>
</managed-bean>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...