PRIMEFACES——如何通过点击来刷新 ?

时间:2022-04-16 20:01:39

I'm considering this showcase: http://www.primefaces.org/showcase/ui/dynamicImage.jsf in particular the sub-case "GraphicText on-the-fly".

我正在考虑这个展示:http://www.primefaces.org/showcase/ui/dynamicImage.jsf,特别是子案例“GraphicText on- fly”。

My problem is implementing an extended version of this sub case with the addition of a . When the button is pressed i need that the image change dinamically.

我的问题是通过增加a实现这个子案例的扩展版本。当按下按钮时,我需要图像以某种方式改变。

In the DynamicImageController class I re-writed the getter associeted with the graphicImage:

在DynamicImageController类中,我重写了与graphicImage关联的getter:

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 the image:

和图片:

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

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

idForm是包含我的graphicImage和我的命令按钮的表单id

My question is:

我的问题是:

Why if I press the F5 button on the keyboard the image change random consistent with the desired behavior in the getGraphicText method ? And why when I press the button the image doesn't change?

为什么如果我按键盘上的F5按钮,图像就会随机变化,与getGraphicText方法中的预期行为一致?为什么当我按下按钮时图像没有变化?

Thanks.

谢谢。

ps. my real problem is the integration of jcaptcha in primefaces, my integration is almost termineted, i miss only the refresh button for the captcha image

我真正的问题是jcaptcha在primefaces中的集成,我的集成几乎被终止,我只漏掉了captcha映像的refresh按钮

2 个解决方案

#1


10  

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

默认情况下,graphicImage缓存图像。在您的p:graphicImage上将缓存属性设置为false,如下所示:

#2


2  

Ok update: the problem is a bug of primefaces I discussed here the problems:

好的更新:这个问题是我在这里讨论过的一个原始面孔的错误问题:

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

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

#1


10  

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

默认情况下,graphicImage缓存图像。在您的p:graphicImage上将缓存属性设置为false,如下所示:

#2


2  

Ok update: the problem is a bug of primefaces I discussed here the problems:

好的更新:这个问题是我在这里讨论过的一个原始面孔的错误问题:

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

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