PRIMEFACES - How to update <p: ​​graphicImage> by clicking the <p: ​​commandButton> button?

I am looking at this showcase: http://www.primefaces.org/showcase/ui/dynamicImage.jsf , in particular in the "GraphicText on the fly" sub-band.

My problem is to implement an extended version of this helper case with the addition of a. When the button is pressed, I need the image to change dynamically.

In the DynamicImageController class, I re-typed the getter associated with graphicsImage:

public StreamedContent getGraphicText(){ double random = Math.random();// a double value with a positive sign, greater than or equal to 0.0 and less than 1.0. if(random>0.5){ BufferedImage bufferedImg = new BufferedImage(100, 25, BufferedImage.TYPE_INT_RGB); Graphics2D g2 = bufferedImg.createGraphics(); g2.drawString("This is a text", 0, 10); ByteArrayOutputStream os = new ByteArrayOutputStream(); try { ImageIO.write(bufferedImg, "png", os); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } graphicText = new DefaultStreamedContent(new ByteArrayInputStream(os.toByteArray()), "image/png"); } else { BufferedImage bufferedImg = new BufferedImage(100, 25, BufferedImage.TYPE_INT_RGB); Graphics2D g2 = bufferedImg.createGraphics(); g2.drawString("This is another text", 0, 10); ByteArrayOutputStream os = new ByteArrayOutputStream(); try { ImageIO.write(bufferedImg, "png", os); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } graphicText = new DefaultStreamedContent(new ByteArrayInputStream(os.toByteArray()), "image/png"); } return graphicText; } 

I have this button:

 <p:commandButton id="refreshImageButton" value="Refresh image random"> <p:ajax update=":idForm" /> </p:commandButton> 

and image:

 <p:graphicImage value="#{dynamicImageController.graphicText}" /> 

idForm is the form id of the form containing my graphicImage and my commandButton

My question is:

Why, if I press the F5 button on the keyboard, accidentally changing the image is consistent with the desired behavior of the getGraphicText method? And why, when I press the button, the image does not change?

Thanks.

ps. my real problem is the integration of jcaptcha into the price lists, my integration almost suffers, I only skip the update button for the captcha image.

+6
source share
2 answers

By default, graphicImage caches the image.
Set the cache attribute to false on p:graphicImage
Something like that:
<p:graphicImage value="#{dynamicImageController.graphicText}" cache="false" />

+9
source

Ok update: the problem is an error in the elements discussed here:

http://forum.primefaces.org/viewtopic.php?f=3&t=35637&p=113830#p113830

+2
source

All Articles