You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@wicket.apache.org by Ceki Gulcu <ce...@qos.ch> on 2009/10/21 19:57:12 UTC

Idiomatic way to reference shared images?

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);
   }
}

In markup, I attempt to access the images as

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

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?

Many thanks in advance for your response,

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

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


Re: Idiomatic way to reference shared images?

Posted by John Krasnay <jo...@krasnay.ca>.
On Thu, Oct 22, 2009 at 09:15:35AM +0200, Ceki Gulcu wrote:
> 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.

Not sure what you mean. The Image/ResourceReference solution works fine across
packages and even across modules, e.g. your images could be part of a shared
library and used by a component in your application.

> 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.

Ah, I see, you want to put it there. Is there a technical reason for this or is
it just a preference? Seems to me to be a lot less flexible than simply letting
your images live on the classpath, since you lose the ability to later package
the images in a shared JAR.

jk


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


Re: Idiomatic way to reference shared images?

Posted by Ceki Gulcu <ce...@qos.ch>.
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-unsubscribe@wicket.apache.org
For additional commands, e-mail: users-help@wicket.apache.org


Re: Idiomatic way to reference shared images?

Posted by John Krasnay <jo...@krasnay.ca>.
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

> Many thanks in advance for your response,
>
> -- 
> Ceki Gülcü
> Logback: The reliable, generic, fast and flexible logging framework for Java.
> http://logback.qos.ch
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
> For additional commands, e-mail: users-help@wicket.apache.org
>

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