views:

750

answers:

3

Hey!

I'm trying to figure out the directory structure of wicket. I don't get it at all! Let's say I wanna load an image with images/logo.gif or something like that. Where do I have to place the images folder? Please help me with this! ;)

A: 

In Netbeans it's under Source Packages tld.domain.project There you make a new folder called images and it works

shevron
+2  A: 

You can create image folder directly in root of your project. i.e. your war archive must contain META-INF, WEB-INF, images at root level. Or in root of your web app folder in servlet container. Then referense it in your html's like

<img src="images/pic1.jpg"/>

If your referensing from java code, you must get it relative to your servlet path. It can be acessed through servlet context by

class MyPage extends WebPage {
  public MyPage() { 
     final ServletContext ctx = ((WebApplication) getApplication()).getServletContext();
     File imgFile = new File(ctx.getRealPath("/images/pic1.jpg"));
   }
}
Alexey Sviridov
Can you explain this a littler bit more? :) That would be great!
shevron
opps... editor eat my post in tags. fixed and little explained.
Alexey Sviridov
+3  A: 

Wicket promotes thinking entire web pages through OOP paradigms and that includes recource handling.

So, if your SomePage extends WebPage is in package myprogram.view.pages, you should most likely add images etc. to myprogram.view.pages.assets (or other similarly named, logical package) and then add the image in the SomePage source by calling

add(new Image("id", new ResourceReference(this.getClass(), "assets/logo.gif")));

This way you will have all your pages, its components and of course related assets in reasonable structure (am I the only one annoyed that people still cram all their CSS stylings to one huge stylesheet?) which is detached from everything else.

And now the advanced parts: For static elements on your page you most likely don't want to do the above since it's obviously somewhat heavyweight way to do it and you may actually want to allow the designer guy to decide which logo image to use. For these sort of things, use

<wicket:link>
    <img src="images/logo.gif" />
</wicket:link>

straight in the markup file. The wicket:link tag will automatically link the image resource pointing to the correct file (path is relevant to the SomePage.class!) and even automagically handles caching.

Esko