Wicket Dynamic Resource Displayed in HTML

2019-06-02 15:38发布

问题:

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