Wicket предоставляет встроенный компонент Image для вывода изображений. Единственный его недостаток заключается в том, что все изображения должны располагаться в той же папке, где и HTML-файлы страницы, на которой хотим вывести изображение. Это не всегда удобно. Ниже представлен код простого компонента, который позволяет отобразить на странице статическое изображение с указанием пути относительно контекста приложения.
public class StaticImage extends WebComponent { private Integer width; private Integer height; public StaticImage(String id, String imageUrl) { super(id, new Model(imageUrl)); } public StaticImage(String id, String imageUrl, int width, int height) { this(id, imageUrl); this.width = width; this.height = height; } protected void onComponentTag(ComponentTag tag) { super.onComponentTag(tag); checkComponentTag(tag, "img"); tag.put("src", getDefaultModelObjectAsString()); if (width != null) tag.put("width", width); if (height != null) tag.put("height", height); } public StaticImage setWidth(int width) { this.width = width; return this; } public StaticImage setHeight(int height) { this.height = height; return this; } }
Пример использования:
public class StaticImageTestPage extends WebPage { public StaticImageTestPage() { add(new StaticImage("staticImage", "images/test.gif").setWidth(100).setHeight(200)); } }