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
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
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 ?
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
Just created an account <— This is me