Wicket Dynamic Resource Displayed in HTML

╄→尐↘猪︶ㄣ 提交于 2019-12-08 07:39:23

问题


I have a Wicket page which will dynamically display an image. Let's say the image is the current date/time.

In HTML, I would have something like

<img src="[dynamically generated]"/>

Assume I have a utility method which returns to me a byte[] of the current image.

public byte[] getCurrImage();

How do I implement this dynamic resource in Wicket (HTML/Java)? I can't mount a shared resource because the image will keep changing all the time. Do I need to save the image somewhere?

Thanks


回答1:


Checkout JFreeChart and wicket example. There you will see an example of a class that derives from Image and it gets a byte[] to dynamically create an image like you asked.




回答2:


This the way I've done it (wicket 1.5). First, your markup:

<img wicket:id="mmFigure" />

And then the code:

add(new NonCachingImage("mmFigure", new AbstractReadOnlyModel<DynamicImageResource>(){
  @Override public DynamicImageResource getObject() {
    DynamicImageResource dir = new DynamicImageResource() {
      @Override protected byte[] getImageData(Attributes attributes) {
        return getCurrImage();
      }
    };
    dir.setFormat("image/png");
    return dir;
  }
}));


来源:https://stackoverflow.com/questions/9687688/wicket-dynamic-resource-displayed-in-html

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!