Hi John,

Thank you for your answer. I was already aware of the idiomatic way for referencing packaged resources. It is a nice way for bundling images which are used within a package. My question was about images shared among multiple packages.

Igor VaynBerg suggested adding a ContextImage which is what I was looking for. Another person (alankila) suggested images defined by ContextRelativeResource.

Here is sample code:

public class MyPage extends WebPage {
  public Contact() {
    ContextImage ci = new ContextImage("helpImage", "images/help.gif");
    add(ci);
  }
}

Associated markup:

   <img wicket:id="helpImage" src=""/>

Of course the interesting part is that the "help.gif" file is located as a resource of my web-app and *not* part of WEB-INF/lib or WEB-INF/classes.

HTH,

John Krasnay wrote:
On Wed, Oct 21, 2009 at 07:57:12PM +0200, Ceki Gulcu wrote:
Hello,

I am trying to defined shared images in a Wicket application.

In my prokect, the image file "help.gif" is located under the
src/main/java/com/foo/ folder of my project. I have created an empty
class called Images.

package com.foo;
public class Images {
}

In the init() method of my web-application, I add help.gif as a shared resource:

public class MyApplication extends WebApplication {


  @Override
  protected void init() {
    ...
    PackageResource pr = PackageResource.get(Images.class, "help.gif");
    sharedResources.add("help.gif", pr);
  }
}


I normally don't need to do anything in my app's init() method for images.

In markup, I attempt to access the images as

   <wicket:link>
     <td><img src="/resources/help.gif" align="top"/></td>
   </wicket:link>


You would use <wicket:link> when the image is in the same package as the
markup. In this case, you would just put in <img src="help.gif"> and Wicket
will re-write the src attribute to the right value. This works well if you like
to preview your markup in a browser.

Since your images are (I think) in a different package, you should get rid of
the <wicket:link> tag.

(Actually, I think using a relative path to the right package in src might
work with <wicket:link>, but I never do it that way. See below.)

Unfortunately, this does not seem to work. However, the following
markup works just fine but it's too cumbersome to write.

   <wicket:link>
     <img src="resources/org.apache.wicket.Application/help.gif"/>
   </wicket:link>

Reading page 229 of the Wicket in Action book, I would have thought
that the "/resources/help.gif" reference would have worked. Quoting
from the book:

  The resource is then available through a stable URL (/resources/discounts),
  independent of components. (page 229)

What is the idiomatic way in Wicket to reference shared images?


Here's my idiom. First, in the same package as my images, I create a class that
extends ResourceReference:

public class MyImage extends ResourceReference {
    public MyImage(String name) {
        super(MyImage.class, name);
    }
}

Then, I attach an Image component to the <img> tag:

    <img wicket:id="smiley"/>

    add(new Image("smiley", new MyImage("smiley.gif")));

No code needed in Application.init(), and no <wicket:link> tags required.

jk

--
Ceki Gülcü
Logback: The reliable, generic, fast and flexible logging framework for Java.
http://logback.qos.ch

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscr...@wicket.apache.org
For additional commands, e-mail: users-h...@wicket.apache.org

Reply via email to