Home > Java, wicket > Wicket Image tag linking to an external resource

Wicket Image tag linking to an external resource

Just a quick tip this time:

Are you in need of creating a Wicket image component linking to an external image ? (an image hosted on a CDN or on an external well defined url ?); Maybe you have to determine the effective url at runtime or maybe you have your reason to not to use a static img tag directly.. whatever your reasons can be, Wicket gives you all the power, and the answer is really simple: enter the realm of StaticImage component:

public class StaticImage extends WebComponent
{
  /**
  * @param id wicket id on the page
  * @param model reference the external URL from which the image is gotten
  *          for ex.: "http://images.google.com/img/10293.gif"
  */
  public StaticImage(String id, IModel urlModel)
  {
    super( id, urlModel );
  }

  protected void onComponentTag(ComponentTag tag)
  {
    super.onComponentTag( tag );
    checkComponentTag( tag, "img" );
    tag.put( "src", getDefaultModelObjectAsString() );
  }
}

Using it is very simple:

You have your markup: (StaticImageTestPage.html)

<img wicket:id="imagetest" />


..And your code (StaticImageTestPage.java)

public class StaticImageTestPage extends WebPage {
  public StaticImageTestPage() {
      add( new StaticImage( "imagetest",
              new Model( "http://whatever.url/image.png" ) ) );
  }
}


That’s a really simple and trivial example, but I hope that you got the picture.

Advertisement
Categories: Java, wicket
  1. Arjun Dhar
    November 8, 2010 at 05:28 | #1

    Hi,
    this has been very convenient for me to use and I appreciate the simplicity (usually I see a lot of Resource Streaming type code in wicket and this is much elegant). But, recently I’ve run into some trouble with the use.

    http://apache-wicket.1842946.n4.nabble.com/Resource-caching-Deployment-Mode-Re-visited-td3027244.html#a3031498

    Maybe I’m not using it correctly. Either way you help on it would be appreciated.
    thanks

  2. November 8, 2010 at 14:01 | #2

    Hi Arjun,
    I’ve personally never run in the problem you describe, probably because It never happened to me to change image files on-the-fly (we had to go through a complete QA acceptance, redeployment and restart of the servers between our development cycles).

    The code of the component per-se does not determine any lock on the file (because you’re managing it at a very high level).
    What Application Server and version are you using at this time ?

  3. Arjun Dhar
    November 9, 2010 at 04:09 | #3

    I test develop it on Jetty but deploy it on Tomcat 6.

    …Maybe it is to do with Wickets architecture and internal resource handling. I posted it on the Wicket User Forum hoping I’d get to know more about caching, resource handling etc. I’ll dig deeper.

    thanks

  4. November 9, 2010 at 08:14 | #4

    Just created an account <— This is me :)

  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.