Sometimes it is desirable to serve images from a separate server rather than the one that the Wicket application is running on for better performance. This can be achieved by a few different techniques...
// NOTE: overriding the "onComponentTag" method will not work when adding the image to an Ajax target (target.addComponent(dynamicImage);). This is why an AttributeModifier is used instead
dynamicImage = new Image("img-status");
dynamicImage.add(new AttributeModifier("src", true, new AbstractReadOnlyModel() {
private static final long serialVersionUID = 1L;
/**
* {@inheritDoc}
*/
@Override
public final Object getObject() {
// based on some condition return the image source
if (myCondition) {
return "http://wicket.apache.org/style/wicket.png";
} else {
return "http://wicket.apache.org/style/apache.png";
}
}
}));
dynamicImage.setOutputMarkupId(true);
|
public class StaticImage extends WebComponent {
public StaticImage(String id, IModel model) {
super(id, model);
}
protected void onComponentTag(ComponentTag tag) {
super.onComponentTag(tag);
checkComponentTag(tag, "img");
tag.put("src", getModelObjectAsString());
// since Wicket 1.4 you need to use getDefaultModelObjectAsString() instead of getModelObjectAsString()
}
}
add(new StaticImage("img", new Model("http://foo.com/bar.gif"));
|
public class ExternalImageUrl extends WebComponent {
public ExternalImageUrl(String id, String imageUrl) {
super(id);
add(new AttributeModifier("src", true, new Model(imageUrl)));
setVisible(!(imageUrl==null || imageUrl.equals("")));
}
protected void onComponentTag(ComponentTag tag) {
super.onComponentTag(tag);
checkComponentTag(tag, "img");
}
}
|
public class GridThumbnailPanel extends Panel {
public GridThumbnailPanel( String id, String label, String iconURL, String linkURL) {
super( id);
Label label = new Label( "label", label);
add( label);
StaticImage img=new StaticImage( "icon", new Model(iconURL));
ExternalLink link=new ExternalLink( "link", linkURL);
link.add(img);
add( link);
}
}
|
<wicket:panel>
<div wicket:id="label">[test]</div>
<a wicket:id="link" target="_blank"> <img wicket:id="icon"/> </a>
</wicket:panel>
|