我考虑了这个展示: http://www.primefaces.org/showcase/ui/dynamicImage.jsf ,特别是子案例“on-the-fly的GraphicText”。
我的问题是在这个子案例的扩展版本中添加一个按钮。当按钮被按下时,我需要图像动态更改。
在DynamicImageController类中,我重写了与graphicImage相关联的getter方法:
我有一个按钮:
我的问题是在这个子案例的扩展版本中添加一个按钮。当按钮被按下时,我需要图像动态更改。
在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;
}
我有一个按钮:
<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方法中所需的行为一致? 而当我按下按钮时,图像为什么不会更改?
谢谢。
附言:我的真正问题是在primefaces中集成jcaptcha,我的集成几乎完成,我只缺少用于刷新验证码图像的按钮。