PRIMEFACES - 如何通过单击<p:commandButton>刷新<p:graphicImage>?

 摩奇甜品 发布于 2023-02-07 14:21

我正在考虑这个展示:http://www.primefaces.org/showcase/ui/dynamicImage.jsf,特别是子版"GraphicText on the the-fly".

我的问题是实现这个子案例的扩展版本,增加了一个.当按下按钮时,我需要图像改变.

在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;
 }

我有这个按钮:


                
             

和图像:


idForm是包含我的graphicImage和commandButton的表单的表单id

我的问题是:

为什么我按下键盘上的F5按钮,图像随机变化与getGraphicText方法中的所需行为一致?为什么当我按下按钮时图像不会改变?

谢谢.

PS.我真正的问题是jcaptcha在primefaces中的集成,我的集成几乎是有限的,我只想念验证码图像的刷新按钮

1 个回答
  • 默认情况下,graphicImage缓存图像.
    cache属性设置为 像这样falsep:graphicImage
    东西:
    <p:graphicImage value="#{dynamicImageController.graphicText}" cache="false" />

    2023-02-07 14:22 回答
撰写答案
今天,你开发时遇到什么问题呢?
立即提问
热门标签
PHP1.CN | 中国最专业的PHP中文社区 | PNG素材下载 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有