【发布时间】:2014-01-17 17:16:07
【问题描述】:
我正在考虑这个展示:http://www.primefaces.org/showcase/ui/dynamicImage.jsf 特别是子案例“GraphicText on-the-fly”。
我的问题是通过添加一个 .当按下按钮时,我需要图像动态变化。
在 DynamicImageController 类中,我重新编写了与图形图像相关的 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;
}
我有这个按钮:
<p:commandButton id="refreshImageButton" value="Refresh image random">
<p:ajax update=":idForm" />
</p:commandButton>
还有图片:
<p:graphicImage value="#{dynamicImageController.graphicText}" />
idForm 是包含我的graphicImage 和我的commandButton 的表单的表单ID
我的问题是:
为什么如果我按下键盘上的 F5 按钮,图像会随机更改与 getGraphicText 方法中所需的行为一致? 为什么当我按下按钮时图像没有改变?
谢谢。
ps。我真正的问题是 jcaptcha 在 primefaces 中的集成,我的集成几乎终止了,我只错过了验证码图像的刷新按钮
【问题讨论】:
-
谁在调用 getStream 方法?
-
我编辑了,不是getStream而是getGraphicText
标签: ajax jsf-2 primefaces captcha page-refresh